day40

一、子查询

子查询是将一个查询语句嵌套在另一个查询语句中。

内层查询语句的查询结果,可以为外层查询语句提供查询条件。

子查询中可以包含:in ,not in ,any,all,exists 和 not exists等关键字

还可以包含比较运算符:=、!=,>,<

二、navicat 软件的应用

三、pymysql模块

improt pymysql

conn=pymysql.connect(host='localhost',user='root',password='1',database='db1','charset='utf8')

coursor=conn.cursor()

sql='select * from user where name="%s" and password="%s"'%(user,pwd)

print(sql)

res=cursor.execute(sql)#执行sql语句,返回sql查询成功的记录数目

print(res)

coursor.close()

conn.close()

 

if res:

  print('登录成功')

else:

  print('登录失败)

二、execute()之sql注入

符号--会注释掉它之后的sql,正确的语法,--后至少有一个任意字符

根本原理,就根据程序的字符串拼接name='%s',我们输入一个xx'--hh

就是用xx加‘在程序中模拟拼接一个判断条件name='xx'--aa'

sql注入:用户存在,绕过密码

egon'--任意字符

sql注入:用户不存在,绕过用户与密码

xx' or '1=1'--任意字符

解决方法,

原来是我们对sql进行字符串进行拼接

现在execute帮我们做了字符串拼接,我们不用在sql语句中为%s加引号

res=cursor.execute(sql,[user,pwd])

scroll绝对移动 absolute
scroll相对移动 relative

 cursor.fetchone()#查看第一条记录

cursor.fetchmany(2)#从第一条开始查两条

cursor.fetchall()查看剩下的所有记录

增:

 conn.cursor()

sql='insert into user(name,pwd) value('root','123')'

res=cursor.execute(sql)

conn.commit():#只有提交后才能发现表中插入记录成功

cursor.close()

conn.close()

猜你喜欢

转载自www.cnblogs.com/lg04551/p/9024681.html