11 May 18
一、 上节课复习
select concat_ws(":",name,age,sex,post) as info from emp; # egon:male:18
二、 子查询(一个问题一个问题解决)
把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询
select name from emp where dep_id = (select id from dep where name="技术"); #子查询
select emp.name from emp inner join dep on emp.dep_id = dep.id where dep.name="技术"; #链表
#查询平均年龄在25岁以上的部门名
select name from dep where id in (select dep_id from emp group by dep_id having avg(age) > 25); #子查询
select dep.name from emp inner join dep on emp.dep_id = dep.id group by dep.name having avg(age) > 25; #链表
#查看不足2人的部门名(子查询得到的是有人的部门id)
select * from emp where exists (select id from dep where id > 3); #exists用法,当()返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询(empty set)
#查询每个部门最新入职的那位员工
select t1.id,t1.name,t1.post,t1.hire_date,t2.post,t2.max_date from emp as t1 inner join (select post,max(hire_date) as max_date from emp group by post) as t2 on t1.post = t2.post where t1.hire_date = t2.max_date;
三、 IDE工具(navicat)介绍
1、 ER图表(Entity-Relationship)
2、 模型;导出sql
3、 查询; 格式美化sql
四、 pymysql模块(安装与查询)
1、安装pymysql(python专用的mysql客户端套接字)
pip3 install pymysql
2、mysql 查询
import pymysql
conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='jxtz0927',
database='db40',
charset='utf8' #防止乱码
)
cursor=conn.cursor(pymysql.cursors.DictCursor) #将产生的结果以字典的形式显示 [{'Tables_in_db40': 'department'}, {'Tables_in_db40': 'employee'}]
# cursor=conn.cursor() # (('department',), ('employee',))
# rows=cursor.execute('show tables;') #rows 为受影响的行数
rows=cursor.execute('select * from class;')
print(rows) #2
print(cursor.fetchone()) #一条
print(cursor.fetchmany(2)) #几条
print(cursor.fetchall()) #全部
cursor.scroll(3,'absolute') #mode='absolute',绝对模式,从最开始位置向后移三条
cursor.scroll(1,'relative') #mode='relative',相对模式,从当前位置向后移一条
cursor.close()
conn.close()
五、 pymysql模块(防止sql注入问题)
1、错误做法, 自行对字符串进行拼接,引发sql注入问题 (name= egon' --asdfg; name=xxxx'or 1=1--asdfg)
import pymysql
conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db42',
charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)
inp_user=input('用户名>>:').strip() #inp_user=""
inp_pwd=input('密码>>:').strip() #inp_pwd=""
sql="select * from user where username='%s' and password='%s'" %(inp_user,inp_pwd) #自行拼接,引发sql注入问题
print(sql)
rows=cursor.execute(sql)
if rows:
print('登录成功')
else:
print('登录失败')
cursor.close()
conn.close()
2、在服务端防止sql注入问题:不要自己拼接字符串,让pymysql模块去拼接
import pymysql
conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db42',
charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)
inp_user=input('用户名>>:').strip() #inp_user=""
inp_pwd=input('密码>>:').strip() #inp_pwd=""
sql="select * from user where username=%s and password=%s"
print(sql)
rows=cursor.execute(sql,(inp_user,inp_pwd))
if rows:
print('登录成功')
else:
print('登录失败')
cursor.close()
conn.close()
六、 pymysql模块(增删改)
import pymysql
conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db42',
charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)
sql='update user set username="alexSB" where id=2'
rows=cursor.execute(sql) #改数据
print(rows)
print(cursor.lastrowid)
sql='insert into user(username,password) values(%s,%s)'
rows=cursor.executemany(sql,[('lwz','123'),('evia','455'),('lsd','333')]) #一次插入多行记录
print(rows)
print(cursor.lastrowid) #显示插到哪行了(id)
conn.commit() # 只有commit提交才会完成真正的修改
cursor.close()
conn.close()