PyMysql
什么是PyMysqlPyMysql是一个纯 Python 实现的 MySQL 客户端库,允许你在 Python 步伐中与 MySQL 数据库进行交互。
安装PyMysql
PyMysql地址:https://pypi.org/project/PyMySQL/
pip install pymysql https://i-blog.csdnimg.cn/direct/0a5383cdb61e4be1afa7236960ef650a.png
利用PyMysql
毗连mysql
import pymysql
# 数据库连接参数
host = '192.168.189.71' # 数据库主机地址
port = 3306 # 数据库端口
user = 'root' # 数据库用户名
password = '123456' # 数据库密码
database = 'test' # 数据库名称
charset = 'utf8mb4' # 编码格式
# 连接到数据库
connection = pymysql.connect(
host=host,
port=port,
user=user,
password=password,
database=database,
charset=charset,
# cursorclass=pymysql.cursors.DictCursor # 使用字典类型游标
)
print("连接成功")
查询数据
# 创建游标对象
cursor = connection.cursor()
# 执行SQL查询
sql = "select * from table_test;"
cursor.execute(sql)
# 获取查询结果
results = cursor.fetchall()
for row in results:
print(row)
# 关闭游标
cursor.close() 查询打印到的数据
[*]元组类型数据
https://i-blog.csdnimg.cn/direct/b24193e56fe04d67b4f370ff61f3ace5.png
[*]字典类型数据
https://i-blog.csdnimg.cn/direct/af7ec716a667416697544c3cc0092fdf.png
添加数据
try:
# 创建游标对象
cursor = connection.cursor()
# 编写sql语句
sql = "insert into table_test (id, name, age) VALUES (%s, %s, '22')"
values = (3, 'Spike')# sql参数
# 执行SQL插入
cursor.execute(sql, values)
# 提交事务
connection.commit()
print("数据插入成功")
except Exception as e:
# 出现异常,回滚事务
connection.rollback()
finally:
# 关闭游标
cursor.close()
# 关闭连接
connection.close() 更新数据
try:
# 创建游标对象
cursor = connection.cursor()
# 编写sql语句
sql = "UPDATE table_test SET age = %s WHERE id = %s"
values = (30, 3)
# 执行SQL
cursor.execute(sql, values)
# 提交事务
connection.commit()
print("数据更新成功")
except Exception as e:
# 出现异常,回滚事务
connection.rollback()
finally:
# 关闭游标
cursor.close()
# 关闭连接
connection.close() 删除数据
try:
# 创建游标对象
cursor = connection.cursor()
# 编写sql语句
sql = "DELETE FROM table_test WHERE id = %s"
value = (3,)
# 执行SQL
cursor.execute(sql, value)
# 提交事务
connection.commit()
print("数据删除成功")
except Exception as e:
# 出现异常,回滚事务
connection.rollback()
finally:
# 关闭游标
cursor.close()
# 关闭连接
connection.close() 利用上下文管理器
自动关闭数据库毗连、自动关闭游标
import pymysql
# 数据库连接参数
host = '192.168.xxx.xxx' # 数据库主机地址
port = 3306 # 数据库端口
user = 'root' # 数据库用户名
password = '******' # 数据库密码
database = 'test' # 数据库名称
charset = 'utf8mb4' # 编码格式
# 使用上下文管理器连接到数据库
with pymysql.connect(
host=host,
port=port,
user=user,
password=password,
database=database,
charset=charset
) as connection:
with connection.cursor() as cursor:
# 编写sql语句
sql = "insert into table_test (id, name, age) VALUES (%s, %s, '22')"
values = (3, 'Spike')# sql参数
try:
# 执行SQL
cursor.execute(sql, values)
# 提交事务
connection.commit()
print("执行成功")
except Exception as e:
print("程序异常")
connection.rollback()
游标常用方法
实行一条sql语句
语法:
cursor.execute(query, args=None)
[*]query是要实行的SQL语句
[*]args是SQL语句中的参数
示例:
cursor.execute("insert into table_test (id, name, age) VALUES (%s, %s, '22')", (3, 'Spike')) 实行多条sql语句
语法:
cursor.executemany(query, args)
[*]query是要实行的SQL语句
[*]args是一个包罗多组参数的列表。
示例:
sql = "INSERT INTO table_test (id, name, age) VALUES (%s, %s, %s)"
values = [(1, 'Tom', 20), (2, 'Jerry', 18), (3, 'Spike', 25)]
cursor.executemany(sql, values) 查询一条数据
语法:
cursor.fetchone()
示例:
result = cursor.fetchone()
print(result) 查询多条数据
语法:
cursor.fetchall()
示例:
results = cursor.fetchall()
for row in results:
print(row)
查询返回效果中指定的行数
语法:
cursor.fetchmany(size)
示例:
# 获取查询结果中的前两行
results = cursor.fetchmany(2)
for row in results:
print(row)
受影响的行数
语法:
cursor.rowcount
[*]比方实行 INSERT、UPDATE 或 DELETE 操作后影响的行数。
示例:
cursor.execute(sql, value)
# 受影响行数
print(cursor.rowcount) 获取末了插入行的ID
语法:
cursor.lastrowid
[*]获取末了插入行的ID(通常用于 AUTO_INCREMENT 字段)
示例:
cursor.execute(sql, value)
print(cursor.lastrowid)# 输出最后插入行的ID
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]