标签:
PyMySQL介绍
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。
Django中也可以使用PyMySQL连接MySQL数据库。
PyMySQL安装
pip install pymysql
连接数据库
在进行本文以下内容之前需要注意:
你有一个MySQL数据库,并且已经启动。
你有可以连接该数据库的用户名和密码
你有一个有权限操作的database
基本使用
import pymysql
# 1. 获取用户输入
name = input('请输入用户名:')
pwd = input('请输入密码:')
# 判断用户名和密码是否正确
# 去数据库查询一下 用户输入的用户名和密码是否正确
# 在Python程序中要连接数据库执行SQL语句 --> pymysql模块
# 1. 连接数据库,得到一个连接
conn = pymysql.connect(
host='192.168.16.94',
port=3306,
user='bbn',
# password='123',
database='day43',
charset='utf8'
)
# 2. 获取光标
cursor = conn.cursor()
# 3. 执行SQL语句
# 3.1 得到SQL语句
sql = "select * from userinfo where username=%s and password=%s;" # 按照pymysql模块的写法定义好占位符
print(sql)
# 3.2 使用光标对象执行SQL语句
ret = cursor.execute(sql, [name, pwd]) # 让pymysql模块帮我们拼接sql语句,执行SQL语句
# 关闭
cursor.close()
conn.close()
# 4 得到结果
if ret:
print('登陆成功')
else:
print('登录失败')
3. SQL注入问题
1. 什么是SQL注入?
用户输入的内容有恶意的SQL语句,后端拿到用户输入的内容不做检测直接做字符串拼接,得到一个和预期不一致的SQL语句
2. 如何解决SQL注入?
对用户输入的内容做检测
pymysql内置了这种检测,我们只需要让pymysql帮我们拼接sql语句
ret = cursor.execute(sql, [name, pwd]) # 让pymysql模块帮我们拼接sql语句,执行SQL语句
注意事项
charset=“utf8”,编码不要写成"utf-8"
返回字典格式数据(查),光标移动:
import pymysql
# 1. 连接数据库,得到一个连接
conn = pymysql.connect(
host='192.168.16.94',
port=3306,
user='bbn',
# password='123',
database='day43',
charset='utf8'
)
# 2. 获取光标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)#返回字典格式数据
# 3. 执行SQL语句
# 3.1 得到SQL语句
sql = "select * from userinfo;" # 按照pymysql模块的写法定义好占位符
# 3.2 使用光标对象执行SQL语句
cursor.execute(sql) # 让pymysql模块帮我们拼接sql语句,执行SQL语句
# 查询所有
# ret = cursor.fetchall()
# 查询单条记录
# ret = cursor.fetchone()
# print(ret)
# ret = cursor.fetchone()
# print(ret)
# ret = cursor.fetchone()
# print(ret)
# 查询指定数量的数据
ret = cursor.fetchmany(3)
print(ret)
print(cursor.fetchone())
print(cursor.fetchone())
# cursor.scroll(0, mode='absolute') # 绝对位置,你让光标移动到哪里
# cursor.scroll(-1, mode='relative') # 相对位置,基于光标当前位置移动
print(cursor.fetchone())
# 关闭
cursor.close()
conn.close()
增删改查操作
增
import pymysql
# 1. 连接数据库,得到一个连接
conn = pymysql.connect(
host='192.168.16.94',
port=3306,
user='bbn',
# password='123',
database='day43',
charset='utf8'
)
# 2. 获取光标
cursor = conn.cursor()
# 3. 执行SQL语句
# 3.1 得到SQL语句
sql = "insert into userinfo(username, password) values (%s,%s);" # 按照pymysql模块的写法定义好占位符
# 3.2 使用光标对象执行SQL语句
ret = cursor.execute(sql, ['Eva_J', '456']) # 让pymysql模块帮我们拼接sql语句,执行SQL语句
# 涉及操作数据库的 一定要提交
conn.commit()
# 关闭
cursor.close()
conn.close()
批量执行
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);"
data = [("Alex", 18), ("Egon", 20), ("Yuan", 21)]
try:
# 批量执行多条插入SQL语句
cursor.executemany(sql, data)
# 提交事务
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
cursor.close()
conn.close()
删
import pymysql
# 1. 连接数据库,得到一个连接
conn = pymysql.connect(
host='192.168.16.94',
port=3306,
user='bbn',
# password='123',
database='day43',
charset='utf8'
)
# 2. 获取光标
cursor = conn.cursor()
# 3. 执行SQL语句
# 3.1 得到SQL语句
sql = "delete from userinfo where username=%s;" # 按照pymysql模块的写法定义好占位符
# 3.2 使用光标对象执行SQL语句
ret = cursor.execute(sql, ['alex']) # 让pymysql模块帮我们拼接sql语句,执行SQL语句
# 涉及操作数据库的 一定要提交
conn.commit()
# 关闭
cursor.close()
conn.close()
改
import pymysql
# 1. 连接数据库,得到一个连接
conn = pymysql.connect(
host='192.168.16.94',
port=3306,
user='bbn',
# password='123',
database='day43',
charset='utf8'
)
# 2. 获取光标
cursor = conn.cursor()
# 3. 执行SQL语句
# 3.1 得到SQL语句
sql = "update userinfo set password=%s where username=%s;" # 按照pymysql模块的写法定义好占位符
# 3.2 使用光标对象执行SQL语句
ret = cursor.execute(sql, ['789', 'gold']) # 让pymysql模块帮我们拼接sql语句,执行SQL语句
# 涉及操作数据库的 一定要提交
conn.commit()
# 关闭
cursor.close()
conn.close()
取刚才插入到数据库的id值,插入数据失败回滚
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);"
username = "Alex"
age = 18
try:
# 执行SQL语句
cursor.execute(sql, [username, age])
# 提交事务
conn.commit()
# 提交之后,获取刚插入的数据的ID
last_id = cursor.lastrowid
except Exception as e:
# 有异常,回滚事务
conn.rollback()
cursor.close()
conn.close()
标签: