一、PyMySQL介绍
PyMySQL是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中是使用mysqldb。
PyMySQL安装.
- 创建链接的基本使用.
# 导入pymysql模块 import pymysql # 连接database conn = pymysql.connect( host=“你的数据库地址”, user=“用户名”,password=“密码”, database=“数据库名”, charset=“utf8”) # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示 # 得到一个可以执行SQL语句并且将结果作为字典返回的游标 #cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 定义要执行的SQL语句 sql = """ CREATE TABLE USER1 ( id INT auto_increment PRIMARY KEY , name CHAR(10) NOT NULL UNIQUE, age TINYINT NOT NULL )ENGINE=innodb DEFAULT CHARSET=utf8; #注意:charset='utf8' 不能写成utf-8 """ # 执行SQL语句 cursor.execute(sql) # 关闭光标对象 cursor.close() # 关闭数据库连接 conn.close()
二、sql注入
例子:简单验证功能
再此之前我们需要在数据库中建立数据,从而我们在pycharm中输入里面真正的数据 从而验证数据的正确性。
建库建表
下面例子中 我将使用我建好的库:db= 'xing'
建好的userinfo表
# pip3 install pymysql import pymysql user=input('user>>: ').strip() pwd=input('password>>: ').strip() # 建立链接 conn=pymysql.connect( host='192.168.0.103',#我的IP地址 port=3306, # 不是字符串不需要加引号。 user='root', password='123', db='xing', charset='utf8' ) # 拿到游标 cursor=conn.cursor() # 执行sql语句 sql='select * from userinfo where user = "%s" and pwd="%s"' % (user, pwd) print(sql) res=cursor.execute(sql) print(res) cursor.close() conn.close() # 进行判断 if res: print('登录成功') else: print('登录失败')
输出结果:
但是会有以下问题:输入的SQL 语句被注释了
或者是
这个时候之后 我们可以这样解决
execute帮我们做字符串拼接 # 将以下代码 sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd) res=cursor.execute(sql) # 改为 sql="select * from userinfo where name=%s and password=%s" #%s需要去掉引号,pymysql会自动加上 res=cursor.execute(sql,[user,pwd])
输出结果:
三、pymysql的增删改查
添加多条数据
import pymysql conn = pymysql.connect( host='192.168.0.103', port=3306, user='root', password='123', database='xing', charset='utf8' ) # 获取一个光标 cursor = conn.cursor() # 定义要执行的sql语句 sql = 'insert into userinfo(user,pwd) values(%s,%s);' data = [ ('july', '147'), ('june', '258'), ('marin', '369') ] # 拼接并执行sql语句 cursor.executemany(sql, data) # 涉及写操作要注意提交 conn.commit() # 关闭连接 cursor.close() conn.close()
插入单条数据
import pymysql conn =pymysql.connect( host ='192.168.0.103', port = 3306, user = 'root', password ='123', database ='xing', charset ='utf8' ) cursor =conn.cursor() #获取一个光标 sql ='insert into userinfo (user,pwd) values (%s,%s);' name = 'wuli' pwd = '123456789' cursor.execute(sql, [name, pwd]) conn.commit() cursor.close() conn.close()
获取最新插入数据 (最后一条)
import pymysql # 建立连接 conn = pymysql.connect( host="192.168.0.103", port=3306, user="root", password="123", database="xing", charset="utf8" ) # 获取一个光标 cursor = conn.cursor() # 定义将要执行的SQL语句 sql = "insert into userinfo (user, pwd) values (%s, %s);" name = "wuli" pwd = "123456789" # 并执行SQL语句 cursor.execute(sql, [name, pwd]) # 涉及写操作注意要提交 conn.commit() # 关闭连接 # 获取最新的那一条数据的ID last_id = cursor.lastrowid print("最后一条数据的ID是:", last_id) cursor.close() conn.close()
输出结果为:(因为我之前插入多条记录时,多运行了两次,所有结果下面的这个)
删除操作
1 import pymysql 2 3 # 建立连接 4 conn = pymysql.connect( 5 host="192.168.0.103", 6 port=3306, 7 user="root", 8 password="123", 9 database="xing", 10 charset="utf8" 11 ) 12 # 获取一个光标 13 cursor = conn.cursor() 14 # 定义将要执行的SQL语句 15 sql = "delete from userinfo where user=%s;" 16 name = "june" 17 # 拼接并执行SQL语句 18 cursor.execute(sql, [name]) 19 # 涉及写操作注意要提交 20 conn.commit() 21 # 关闭连接 22 23 cursor.close() 24 conn.close()
更改数据
1 import pymysql 2 3 # 建立连接 4 conn = pymysql.connect( 5 host="192.168.0.103", 6 port=3306, 7 user="root", 8 password="123", 9 database="xing", 10 charset="utf8" 11 ) 12 # 获取一个光标 13 cursor = conn.cursor() 14 # 定义将要执行的SQL语句 15 sql = "update userinfo set pwd=%s where user=%s;" 16 # 拼接并执行SQL语句 17 cursor.execute(sql, ["july", "july"]) 18 19 # 涉及写操作注意要提交 20 conn.commit() 21 22 # 关闭连接 23 cursor.close () 24 conn.close ()
查询数据
fetch数据
1 # 可以获取指定数量的数据 2 cursor.fetchmany(3) 3 # 光标按绝对位置移动1 4 cursor.scroll(1, mode="absolute") 5 # 光标按照相对位置(当前位置)移动1 6 cursor.scroll(1, mode="relative")
数据回滚
1 import pymysql 2 3 # 建立连接 4 conn = pymysql.connect( 5 host="192.168.0.103", 6 port=3306, 7 user="root", 8 password="123", 9 database="xing", 10 charset="utf8" 11 ) 12 # 获取一个光标 13 cursor = conn.cursor() 14 # 定义将要执行的SQL语句 15 sql1 = "insert into userinfo (user, pwd) values (%s, %s);" 16 sql2 = "insert into hobby (id, hobby) values (%s,%s);" 17 user = "july1" 18 pwd = "july1" 19 id = "我是错误的id" #id = "3" 20 hobby = "打游戏" 21 try: 22 # 拼接并执行SQL语句 23 cursor.execute(sql1, [user, pwd]) 24 print(sql1) 25 cursor.execute(sql2, [id, hobby]) # 报错的SQL语句 26 # 涉及写操作注意要提交 27 conn.commit() 28 except Exception as e: 29 print(str(e)) 30 # 有异常就回滚 31 conn.rollback() 32 33 # 关闭连接 34 cursor.close() 35 conn.close()