采用本地的文件路径协议找到的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
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安装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 [(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。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()