使用PyMySQL怎么实现增删查改操作?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
1、PyMySQL的使用步骤:




2、案例:
2.1 查询数据库中的表的信息:
# 需求:查询数据库person中info表的信息
# 1.导包
import pymysql
try:
# 2.连接MySQL数据库的服务
connc = pymysql.Connect(
user="root", # The first four arguments is based on DB-API 2.0 recommendation.
password="4412",
host='127.0.0.1', # mysql服务端的IP,默认是127.0.0.1/localhost,或者写真实的ip
database='person',
port=3306,
charset="utf8")
# 3.创建游标对象
cur = connc.cursor()
# 4.编写SQL语句
sql = 'select * from info;'
# 5.使用游标对象调用SQL
cur.execute(sql)
# 6.获取查询的结果
result= cur.fetchall()
print(result)
# 7.关闭游标对象
cur.close()
# 8.关闭连接
connc.close()
except Exception as e:
print(e)
运行结果:

2.2 增加数据:
大部分的步骤都和前面一样,直接在程序中注释看:
# 需求:
# 增加数据 刘德华56 男 数据 到 数据库person--的info表中
# 修改数据 小王 的名字为 小王吧 到 数据库person--的info表中
# 删除数据 张三 数据库person--的info表中
# 1.导包
import pymysql
# 2.连接MySQL服务
connc = pymysql.Connect(
user="root", # The first four arguments is based on DB-API 2.0 recommendation.
password="4412",
host='127.0.0.1', # mysql服务端的IP,默认是127.0.0.1/localhost,或者写真实的ip
database='person',
port=3306,
charset="utf8")
# 3.创建游标对象
cur = connc.cursor()
try:
# 4.编写、增加、删除的SQL语句
# 增加数据 刘德华 56 男
sql = 'insert into info values(%s, %s, %s, %s)'
add_data = [0,"刘德华", 56, "男"]
# 5.使用游标对象执行SQL语句
cur.execute(sql, add_data)
# 6.提交操作
connc.commit()
except Exception as e:
print(e)
# 操作失败,数据回滚
connc.rollback()
finally:
# 7.关闭游标对象
cur.close()
# 8.关闭连接
connc.close()
print("结束!")
运行之后,看看person数据库中 表info 的数据,确实增加成功了:

2.3 修改数据:
# 需求:
# 增加数据 刘德华56 男 数据 到 数据库person--的info表中
# 修改数据 小王 的名字为 小王吧 到 数据库person--的info表中
# 删除数据 张三 数据库person--的info表中
# 1.导包
import pymysql
# 2.连接MySQL服务
connc = pymysql.Connect(
user="root", # The first four arguments is based on DB-API 2.0 recommendation.
password="4412",
host='127.0.0.1', # mysql服务端的IP,默认是127.0.0.1/localhost,或者写真实的ip
database='person',
port=3306,
charset="utf8")
# 3.创建游标对象
cur = connc.cursor()
try:
# 4.编写、增加、删除的SQL语句
# 修改数据 李四 的名字为 李四的爸爸
sql = 'update info set name=%s where name="李四"'
update_data = ["李四的爸爸"]
# 5.使用游标对象执行SQL语句
cur.execute(sql, update_data)
# 6.提交操作
connc.commit()
except Exception as e:
print(e)
# 操作失败,数据回滚
connc.rollback()
finally:
# 7.关闭游标对象
cur.close()
# 8.关闭连接
connc.close()
print("结束!")
运行之后,看看person数据库中 表info 的数据,确实修改成功了:

2.3 删除数据:
# 需求:
# 增加数据 刘德华56 男 数据 到 数据库person--的info表中
# 修改数据 小王 的名字为 小王吧 到 数据库person--的info表中
# 删除数据 张三 数据库person--的info表中
# 1.导包
import pymysql
# 2.连接MySQL服务
connc = pymysql.Connect(
user="root", # The first four arguments is based on DB-API 2.0 recommendation.
password="4412",
host='127.0.0.1', # mysql服务端的IP,默认是127.0.0.1/localhost,或者写真实的ip
database='person',
port=3306,
charset="utf8")
# 3.创建游标对象
cur = connc.cursor()
try:
# 4.编写、增加、删除的SQL语句
# 修改数据 李四 的名字为 李四的爸爸
sql = 'update info set name=%s where name="李四"'
update_data = ["李四的爸爸"]
# 5.使用游标对象执行SQL语句
cur.execute(sql, update_data)
# 6.提交操作
connc.commit()
except Exception as e:
print(e)
# 操作失败,数据回滚
connc.rollback()
finally:
# 7.关闭游标对象
cur.close()
# 8.关闭连接
connc.close()
print("结束!")
运行之后,看看person数据库中 表info 的数据,确实删除成功了:

关于使用PyMySQL怎么实现增删查改操作问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注天达云行业资讯频道了解更多相关知识。