python pymysql用法_Python使用pymysql模块操作mysql增删改查实例分析

本文详细介绍了如何使用Python的pymysql模块进行MySQL数据库的增删改查操作,包括用户输入避免SQL注入的方法,以及查询数据时的fetchone、fetchall和fetchmany等方法的使用。此外,还强调了在执行增删改操作时必须调用commit()方法以确保数据生效。
摘要由CSDN通过智能技术生成

本文实例讲述了Python使用pymysql模块操作mysql增删改查。分享给大家供大家参考,具体如下:

# -*- coding:utf-8 -*-

import pymysql

user = input("请输入用户名:")

pwd = input("请输入密码:")

# 1.连接

conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123", db="t1", charset="utf8")

print(conn)

# 2.创建游标

cursor = conn.cursor()

#注意%s需要加引号

sql = "select * from t1.userinfo where username="%s" and pwd="%s"" %(user, pwd)

print(sql)

# 3.执行sql语句

cursor.execute(sql)

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

print(result)

# 关闭连接,游标和连接都要关闭

cursor.close()

conn.close()

if result:

print("登陆成功")

else:

print("登录失败")

下面是执行过程

请输入用户名:lisi

请输入密码:123

select * from t1.userinfo where username="lisi" and pwd="123"

登陆成功

二、execute()之sql注入

方式

#1、sql注入之:用户存在,绕过密码

lisi" -- 任意字符

#2、sql注入之:用户不存在,绕过用户与密码

xxx" or 1=1 -- 任意字符请输入用户名:sdj;fja;" or 1=1 -- ;j;j;jj;jjkdsjfjsd

请输入密码:123

select * from t1.userinfo where username="sdj;fja;" or 1=1 -- ;j;j;jj;jjkdsjfjsd" and pwd="123"

登陆成功

解决:

1采用列表的方式

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

# sql="select * from userinfo where name="%s" and password="%s"" %(username,pwd)

# print(sql)

# result=cursor.execute(sql)

#改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)

sql="select * from userinfo where name=%s and pwd=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上

result=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。

2采用字典的方法

# -*- coding:utf-8 -*-

import pymysql

user = input("请输入用户名").strip()

pwd = input("请输入密码").strip()

# 连接服务端

conn = pymysql.connect(

host="127.0.0.1",

user="root",

password="123",

database="t1",

port=3306,

charset="utf8"

)

# -- ddadad

# 创建游标对象

cur = conn.cursor()

sql = "select * from userinfo where username = %(name)s and pwd = %(password)s"

print(sql)

# resultNum = cur.execute(sql,[user,pwd])

resultNum = cur.execute(sql,{"name":user,"password":pwd})

print(resultNum)

cur.close()

conn.close()

if resultNum:

print("登陆成功")

else:

print("登陆失败")

三、增、删、改:conn.commit()

commit()方法:在数据库里增、删、改的时候,必须要进行提交,否则插入的数据不生效。

基本框架

import pymysql

username = input("请输入用户名:")

pwd = input("请输入密码:")

# 1.连接

conn = pymysql.connect(host="localhost", port=3306, user="root", password="123", db="db8", charset="utf8")

# 2.创建游标

cursor = conn.cursor()

--------增删改操作----------------

#一定记得commit

conn.commit()

# 4.关闭游标

cursor.close()

# 5.关闭连接

conn.close()

增--》一组数据

sql="insert into userinfo(username,pwd) values(%s,%s)"

effect_row=cursor.execute(sql,(username,pwd)) # effect_row=1

增---》多组数据

sql="insert into userinfo(username,pwd) values(%s,%s)"

effect_row=cursor.executemany(sql,[("赵八","112"),("刘九","114"),("封十","911")]) #effect_row=3

sql="delete from userinfo where pwd like "%2""

effect_row=cursor.execute(sql)

sql="update userinfo set username = %s where pwd="114""

effect_row=cursor.execute(sql,"niu") #effect_row=2

**上面的变量 effect_row=cursor.execute(...) ,返回的是成功改变的条目数字

四、查:fetchone、fetchmany、fetchall

表的内容

mysql> select * from userinfo;

+----+----------+-----+

| id | username | pwd |

+----+----------+-----+

| 1 | mjj | 123 |

| 3 | 张三 | 110 |

| 4 | 李四 | 119 |

+----+----------+-----+

3 rows in set (0.00 sec)

固定格式

import pymysql

# 1.连接

conn = pymysql.connect(host="localhost", port=3306, user="root", password="", db="db8", charset="utf8")

# 2.创建游标

cursor = conn.cursor()

sql = "select * from userinfo"

cursor.execute(sql)

------------执行的查询--------------

# 4.关闭游标

cursor.close()

# 5.关闭连接

conn.close()

fetchone查看一条符合条件的数据,可以连续使用,查询的是上一个fetchone的后面一条

# 查询第一行的数据

row = cursor.fetchone()

print(row) # (1, "mjj", "123")

# 查询第二行数据

row = cursor.fetchone()

print(row) # (3, "张三", "110")

fetchall():查询所有符合条件的数据

# 获取所有的数据

rows = cursor.fetchall()

print(rows)

#运行结果

((1, "mjj", "123"), (3, "张三", "110"), (4, "李四", "119")) 取到的返回值是元组

fetchmany:获取指定的条数数据

row=cursor.fetchmany(3)

print(row)

cursor.scroll(num,mode="relative|absolute")  当mode=absolute时,num不能小于0

cursor.scroll(1,mode="relative") # 相对当前位置移动

cursor.scroll(2,mode="absolute") # 相对绝对位置移动

# 查询第一行的数据

row = cursor.fetchone()

print(row) # (1, "mjj", "123")

# 查询第二行数据

row = cursor.fetchone() # (3, "张三", "110")

print(row)

cursor.scroll(-1,mode="relative") #设置之后,光标相对于当前位置往前移动了一行,所以打印的结果为第二行的数据

row = cursor.fetchone()

print(row)

cursor.scroll(0,mode="absolute") #设置之后,光标相对于首行没有任何变化,所以打印的结果为第一行数据

row = cursor.fetchone()

print(row)

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python常见数据库操作技巧汇总》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值