欢迎投稿

今日深度:

pymysql,

pymysql,


python代码连接mysql数据库

有bug(sql注入的问题):

#pip3 install pymysql
import pymysql

user=input('user>>: ').strip()
pwd=input('password>>: ').strip()

# 建立链接
conn=pymysql.connect(
    host='192.168.10.15',
    port=3306,
    user='root',
    password='123',
    db='db9',
    charset='utf8'
)

# 拿到游标
cursor=conn.cursor()

# 执行sql语句

sql='select * from userinfo where user = "%s" and pwd="%s"' %(user,pwd)
rows=cursor.execute(sql)

cursor.close()
conn.close()

# 进行判断
if rows:
    print('登录成功')
else:
    print('登录失败')

 

在sql语句中 --空格 就表示后面的被注释了,所以密码pwd就不验证了,只要账户名对了就行了,这样跳过了密码认证就是sql注入

 

 这样的连用户名都不用知道都可以进入,所以在注册账户名时好多特殊字符都不让使用,就怕这个.

 

改进版(防止sql注入):

#pip3 install pymysql
import pymysql

user=input('user>>: ').strip()
pwd=input('password>>: ').strip()

# 建立链接
conn=pymysql.connect(
    host='192.168.10.15',
    port=3306,
    user='root',
    password='123',
    db='db9',
    charset='utf8'
)

# 拿到游标
cursor=conn.cursor()

# 原来的执行sql语句

# sql='select * from userinfo where user = "%s" and pwd="%s"' %(user,pwd)
# print(sql)
# 现在的
sql='select * from userinfo where user = %s and pwd=%s'
rows=cursor.execute(sql,(user,pwd))
#原来是在sql中拼接,现在是让execute去做拼接user和pwd

cursor.close()
conn.close()

# 进行判断
if rows:
    print('登录成功')
else:
    print('登录失败')

 

 

增删改:先安装pymysql模块(pip install pymysql)

 

#1、增删改
import pymysql

# 建立链接
conn=pymysql.connect(
    host='127.0.0.1',#本地连接时就用127.0.0.1,如果远程连接时就查一下对方ip再写进来
    port=3306,
    user='root',
    password='123',
    db='db9',
    charset='utf8'
)

# 拿游标
cursor=conn.cursor()

# 执行sql
# 增、删、改
sql='insert into user(user,pwd) values(%s,%s)'#增删改就自己写sql语句即可,insert,update,drop
#插入一条
# rows=cursor.execute(sql,('wxx','123'))
# print(rows)

#插入多条
# rows=cursor.executemany(sql,[('yxx','123'),('egon1','111'),('egon2','2222')])
# print(rows)

rows=cursor.executemany(sql,[('egon3','123'),('egon4','111'),('egon5','2222')])
print(cursor.lastrowid)#查看你当前id走到了多少,如果是7,那么你再插入数据时,id就从7开始

conn.commit()#必须加这个才能保存,否则增删改不保存
# 关闭
cursor.close()
conn.close()

 

 

查询:

 

# 2、查询
import pymysql

# 建立链接
conn=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123',
    db='db9',
    charset='utf8'
)

# 拿游标
# pymysql.cursors.DictCursor写这个当你拿到查询数据时会以字典形式显示,\
# 字段名是键,字段内容是值,如果不写只写conn.cursor(),\
# 你拿到的就是一个大元组,都是字段的内容,没有字段的名,你不知道是什么数据
cursor=conn.cursor(pymysql.cursors.DictCursor)

# 执行sql
# 查询
# 实际上就是你cursor已经将sql查询的内容已经写到固定地方了,\
# 每次你都是你那里拿数据,拿一次光标走一次,跟读文件很类似
rows=cursor.execute('select * from user;')
print(rows)#只显示受影响行数,而没有其他内容

#取法一,fetchone只拿一条
print(cursor.fetchone())#拿到第一条内容
print(cursor.fetchone())#拿到第二条内容,一次只取一条内容
print(cursor.fetchone())#取空了,再取就会显示None

#取法二,fetchmany拿多条
print(cursor.fetchmany(2))#指定取几条数据

#取法三:fetchall全部拿出
print(cursor.fetchall())#拿出所有内容
print(cursor.fetchall())#再拿就显示[]而不是None

#移动光标,再取
# 现在你可以移动光标,让你取完内容后可以再取一遍
cursor.scroll(3,mode='absolute') # 相对绝对位置移动,
# 相当于从头开始,光标移动到从头数第三行记录的末尾,你再取就从id=4开始取
print(cursor.fetchone())

cursor.scroll(2,mode='relative') # 相对当前位置移动
# 向后跳,如果你当前id=1,你执行后再取就会变成id=4,把2和3两行跳过了
print(cursor.fetchone())

# 关闭
cursor.close()
conn.close()

 

www.htsjk.Com true http://www.htsjk.com/Mysql/42891.html NewsArticle pymysql, python代码连接mysql数据库 有bug(sql注入的问题): # pip3 install pymysql import pymysqluser =input( ' user: ' ).strip()pwd =input( ' password: ' ).strip() # 建立链接 conn= pymysql.connect( host = ' 192.168.10.15 '...
相关文章
    暂无相关文章
评论暂时关闭