SQLyog数据库的使用

django

web
database 数据库
linux nginx

web的集群

打开网页的方式

采用本地的文件路径协议找到的liweiwei.html文件

file:///F:/djangostart/templates/liweiwei.html
这种没有发送HTTP请求,也没有http响应

采用URL的方式

http://127.0.0.1:8000/liweiwei/    ---》django 
发送HTTP请求,也有http响应

django自带的web服务,功能有限。 开发人员的测试使用web功能,—》Apache、nginx

MySQL数据库

MariaDB 是MySQL数据库的分支,和MySQL使用非常相似

Maria是MySQL创始人女儿的名字

1.创建表和库
2.基本的语句(select、insert、update、delete)
3.SQLyog的使用
4.pymysql库的使用

    http://www.runoob.com/python3/python3-mysql.html
5.django里的ORM对数据库的操作
    http://www.runoob.com/django/django-model.html

安装Mariadb

1.安装软件

[root@localhost ~]# yum  install mariadb  mariadb-server
mariadb 是客户端使用Mariadb的软件,例如mysql
mariadb-server 是提供数据库服务的软件。

2.启动

[root@localhost ~]# service   mariadb   start  启动mariadb服务
Redirecting to /bin/systemctl start mariadb.service
[root@localhost ~]#
[root@localhost ~]# service  nginx  start 启动nginx服务

3.查看mariadb相关的进程

[root@localhost ~]# ps aux|grep mariadb
ps aux 查看linux下的所有的进程的命令---》任务管理器
[root@localhost ~]# man ps 查看ps命令的使用手册
    按q退出 quit
grep 是过虑内容的命令
| 是管道符号,作用:就是将前面命令的输出送给后面的命令做为输入使用。中间人

[root@localhost ~]# ps aux|grep "nginx"
root      11072  0.0  0.1 120796  2100 ?        Ss   22:21   0:00 nginx: master process /usr/sbin/nginx
nginx     11073  0.0  0.1 121184  3120 ?        S    22:21   0:00 nginx: worker process
root      11098  0.0  0.0 112704   968 pts/0    S+   22:30   0:00 grep --color=auto nginx
[root@localhost ~]#

安装lsof命令:
作用:
1.可以查看某个端口被哪个进程占用了 -i:80

2.哪个进程一共打开了多少个文件   ---》校招

[root@localhost ~]# yum install lsof  -y   安装
[root@localhost ~]# lsof -i:80     查看80端口被哪个进程占用了
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   11072  root    6u  IPv4  35464      0t0  TCP *:http (LISTEN)
nginx   11072  root    7u  IPv6  35465      0t0  TCP *:http (LISTEN)
nginx   11073 nginx    6u  IPv4  35464      0t0  TCP *:http (LISTEN)
nginx   11073 nginx    7u  IPv6  35465      0t0  TCP *:http (LISTEN)
[root@localhost ~]# lsof -i:3306
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  11016 mysql   14u  IPv4  35106      0t0  TCP *:mysql (LISTEN)
[root@localhost ~]#

进程号:
端口号:
[root@localhost ~]# lsof -p 11016 查看11060进程打开了哪些文件

登录mariadb

mysql是登录mariadb数据库的命令
-u 指定登录数据库的用户名  root也是mariadb数据库里的超级管理员
-p  指定密码 ,默认情况下没有密码,密码为空

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>quit  退出数据库

mariadb的基本操作

1.建库建表
    什么是库?
        库(database):是用来存放表的容器,一个库里可以存放很多的表

    什么是表?
        表(table) ,容器 ,也是用来装数据
                列:字段 field
                行:记录 record
                行 row和列 column
操作数据库的语言:SQL语句
简介:结构化查询语言(Structured Query Language)简称SQL(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。
关系型数据库:mysql、mariadb、oracle
非关系型数据库:redis、MongoDB、memcached   --->NOSQL
        key:value
        字典    studentinfo = {"name":"cali","age":18}

查询有哪些库

MariaDB [(none)]> show  databases;
MariaDB [(none)]> create database  django;
MariaDB [(none)]> use  django
Database changed
MariaDB [django]>
MariaDB [django]> show  tables;  #查看库里有哪些表
Empty set (0.00 sec)

MariaDB [django]> 

创建一个表,里面有很多字段和指定的数据类型

MariaDB [django]> create table  studentinfo(id int primary key,name varchar(20),sex varchar(6),age int,grade decimal(5,2) ,birthday date,phoneNO decimal(20) not null);
Query OK, 0 rows affected (0.01 sec)

MariaDB [django]> show tables;
+------------------+
| Tables_in_django |
+------------------+
| studentinfo      |
+------------------+
1 row in set (0.00 sec)

MariaDB [django]> 
MariaDB [django]> desc  studentinfo;  查看表的结构  description
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| id       | int(11)       | NO   | PRI | NULL    |       |
| name     | varchar(20)   | YES  |     | NULL    |       |
| sex      | varchar(6)    | YES  |     | NULL    |       |
| age      | int(11)       | YES  |     | NULL    |       |
| grade    | decimal(5,2)  | YES  |     | NULL    |       |
| birthday | date          | YES  |     | NULL    |       |
| phoneNO  | decimal(20,0) | NO   |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

MariaDB [django]>

插入数据

MariaDB [django]> insert  into  studentinfo(id,name,sex,age,grade,birthday,phoneNO) values (1,"cali","male",18,60.5,"1984-10-01",18908495097);
Query OK, 1 row affected (0.00 sec)

MariaDB [django]>

查询

MariaDB [django]> select  *  from   studentinfo;
+----+------+------+------+-------+------------+-------------+
| id | name | sex  | age  | grade | birthday   | phoneNO     |
+----+------+------+------+-------+------------+-------------+
|  1 | cali | male |   18 | 60.50 | 1984-10-01 | 18908495097 |
+----+------+------+------+-------+------------+-------------+
1 row in set (0.00 sec)

MariaDB [django]>
2.增删改差语句的使用


进程和进程之间通讯的问题
1.socket  槽
    网络socket:  ip + 端口号    127.0.0.1:8000   192.168.123.59:3306
            不同机器上的不同进程之间的通讯
    文件socket
        本机上不同进程之间的通讯

插入多行数据

test1.save() -->django

MariaDB [tang]> insert into ljj(id,name) values  (2,"rose"),(3,"jack");

MariaDB [django]> insert  into  studentinfo(id,name,sex,age,grade,birthday,phoneNO) values (2,"rose","female",18,60.5,"1984-10-01",18808495097);
Query OK, 1 row affected (0.00 sec)

MariaDB [django]> select  * from studentinfo;
+----+------+--------+------+-------+------------+-------------+
| id | name | sex    | age  | grade | birthday   | phoneNO     |
+----+------+--------+------+-------+------------+-------------+
|  1 | cali | male   |   18 | 60.50 | 1984-10-01 | 18908495097 |
|  2 | rose | female |   18 | 60.50 | 1984-10-01 | 18808495097 |
+----+------+--------+------+-------+------------+-------------+
2 rows in set (0.00 sec)

过滤条件 where后面的是条件

Test.objects.all()  -->select * from studentinfo
Test.objects.filter(id=1)  -->select * from studentinfo where id=1

MariaDB [django]> select  * from studentinfo where sex="female";
+----+------+--------+------+-------+------------+-------------+
| id | name | sex    | age  | grade | birthday   | phoneNO     |
+----+------+--------+------+-------+------------+-------------+
|  2 | rose | female |   18 | 60.50 | 1984-10-01 | 18808495097 |
+----+------+--------+------+-------+------------+-------------+
1 row in set (0.00 sec)

MariaDB [django]> 

MariaDB [django]> select  * from studentinfo where id=2;
+----+------+--------+------+-------+------------+-------------+
| id | name | sex    | age  | grade | birthday   | phoneNO     |
+----+------+--------+------+-------+------------+-------------+
|  2 | rose | female |   18 | 60.50 | 1984-10-01 | 18808495097 |
+----+------+--------+------+-------+------------+-------------+
1 row in set (0.00 sec)

MariaDB [django]> select  * from studentinfo where name="rose";
+----+------+--------+------+-------+------------+-------------+
| id | name | sex    | age  | grade | birthday   | phoneNO     |
+----+------+--------+------+-------+------------+-------------+
|  2 | rose | female |   18 | 60.50 | 1984-10-01 | 18808495097 |
+----+------+--------+------+-------+------------+-------------+
1 row in set (0.00 sec)

MariaDB [django]> 

select 指定部分字段

MariaDB [django]> select name,sex,phoneNO from studentinfo;
+------+--------+-------------+
| name | sex    | phoneNO     |
+------+--------+-------------+
| cali | male   | 18908495097 |
| rose | female |     1234566 |
+------+--------+-------------+
2 rows in set (0.00 sec)

MariaDB [django]> select name from studentinfo;
+------+
| name |
+------+
| cali |
| rose |
+------+
2 rows in set (0.00 sec)

MariaDB [django]> 

更新数据库里的数据

    Test.objects.filter(id=1).update(name='Google')

where条件非常重要,不接会修改所有的行的数据
接了where条件,就只是修改符合条件的行的数据

MariaDB [django]> update  studentinfo set age=35 where id=1;

删除语句

    Test.objects.filter(id=1).delete()

MariaDB [django]> delete from studentinfo where id=2;

SQLyog使用

windows里连接数据库的工具。
需要到mysql(mariadb里授权一个用户能远程连接到数据库)

如何授权?

grant是授权的命令
ALL 表示所有的权限(SELECT INSERT UPDATE DELETE等)
on django.* 在django这个数据库里的所有的表   * 通配符号(表示所有的表)
'cali'@'%'  表示cali这个用户可以从任何一台电脑连接到数据库  % 表示任何一台电脑的ip地址
identified by '123456'  设置密码为123456

MariaDB [(none)]> grant  ALL on  django.*  to   'cali'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]>

注意关闭防火墙

[root@localhost ~]# iptables -F   清除防火墙规则

pymysql模块使用

是在python 3.0里连接到数据库里,操作数据库的。
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。

安装pymysql模块

pip install pymysql

使用pymysql模块去数据库连接

import pymysql
# 打开数据库连接  提供服务器的ip、用户名、密码、具体连接的库
db = pymysql.connect("192.168.123.59", "cali", "123456", "django")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute()  方法执行 SQL 查询
cursor.execute("select * from studentinfo")
# 使用 fetchall() 方法获取所有的数据.
data = cursor.fetchall()
print(data)
# 关闭数据库连接
db.close()

增强版

import pymysql
# 打开数据库连接  提供服务器的ip、用户名、密码、具体连接的库
db = pymysql.connect("192.168.123.59", "cali", "123456", "django")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute()  方法执行 SQL 查询
# cursor.execute("select * from studentinfo")
cursor.execute("select name from studentinfo")
# 使用 fetchall() 方法获取所有的数据.
data = cursor.fetchall()
data2 = []
for i in data:
    print(i[0])
    data2.append(i[0])
print(data2)
# 关闭数据库连接
db.close()

练习:

1.要求用户输入内容:用户名、性别、分数等信息
2.往数据库里studentinfo表里插入多条数据
3.查询数据库是否有?
4.如何判断一个用户是否在studentinfo表里存在了?

扩展的练习

ajax实现后端的验证用户是否存在?

案例

db_test.py 里的代码

import pymysql
from accept_info import info

stuinfo = info()

print(stuinfo,type(stuinfo))
# 打开数据库连接  提供服务器的ip、用户名、密码、具体连接的库
db = pymysql.connect("192.168.123.59", "cali", "123456", "django")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute()  方法执行 SQL 查询
# cursor.execute("select * from studentinfo")

sql = f"insert into studentinfo(id,name,sex,age,grade,birthday,phoneNO) VALUES " \
      f"({stuinfo[0]},'{stuinfo[1]}','{stuinfo[2]}',{stuinfo[3]},{stuinfo[4]},'{stuinfo[5]}',{stuinfo[6]})"
print(sql)
# cursor.execute("select name from studentinfo")
cursor.execute(sql)

# 提交数据写入到数据库  对数据库里的数据进行修改、删除、插入都需要commit
db.commit()
# 使用 fetchall() 方法获取所有的数据.
cursor.execute("select name from studentinfo")
data = cursor.fetchall()
data2 = []
for i in data:
    print(i[0])
    data2.append(i[0])
print(data2)
# 关闭数据库连接
db.close()

accept_info.py里的代码

def  info():
    id = int(input("请输入id:"))
    name = input("请输入name:")
    sex = input("请输入sex:")
    age = int(input("请输入age:"))
    grade = input("请输入grade:")
    birthday = input("请输入birthday:")
    phoneNO = int(input("请输入phoneNO:"))

    return id,name,sex,age,grade,birthday,phoneNO

if __name__ == "__main__":
    info()

猜你喜欢

转载自blog.csdn.net/qq_43002177/article/details/81880540