pymysql连接、关闭、查询,python如何操作mysql数据库

复制代码
 1 def get_conn():
 2     """
 3     :return: 连接,游标
 4     """
 5     # 创建连接
 6     conn = pymysql.connect(host="127.0.0.1",
 7                     user="root",
 8                     password="你的数据库密码",
 9                     db="要使用的数据名称",
10                     charset="utf8")
11     # 创建游标
12     cursor = conn.cursor()  # 执行完毕返回的结果集默认以元组显示
13     return conn, cursor
14 
15 def close_conn(conn, cursor):
16     if cursor:
17         cursor.close()
18     if conn:
19         conn.close()
20 """
21 -----------------------------------------------------------
22 """
23 """
24 ------------------------------------------------------------------------------------
25 """
26 def query(sql,*args):
27     """
28     通用封装查询
29     :param sql:
30     :param args:
31     :return:返回查询结果 ((),())
32     """
33     conn , cursor= get_conn()
34     print(sql)
35     cursor.execute(sql)
36     res = cursor.fetchall()
37     close_conn(conn , cursor)
38     return res
复制代码

 

posted @   靠谱杨  阅读(847)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2020-10-16 JavaDailyReports10_16

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示