python3 Mysql 数据库连接 - Pymysql 驱动 PyMysql 连接数据库,实现增删改查 什么是 PyMySQL? PyMySQL 是在 python3.x 版本中用于连接 MySQ
PyMySQL 是在 python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。
PyMySQL 遵循 Python 数据库 api v2.0 规范,并包含了 pure-Python MySQL 客户端库。
$ pip install PyMySQL
在操作mysql之前,首先要与mysql建立连接
conn=pymysql.connect(host=“mysql域名/ip”,user=“用户名”,passWord=“密码”,db=“库名”,port=端口号3306,charset=‘utf-8’)
当游标建立之时,就自动开始了一个隐形的数据库事务
#使用 cursor() 方法创建一个游标对象 cursor
cursor = conn.cursor()
sql=“select * from user”
cursor.execute(sql) 或 cursor.execute(“select * from user”)
conn.commit()
conn.rollback()方法回滚当前游标的所有操作。每一个方法都开始了一个新的事务
cursor.close()
conn.close()
import pymysql#创建数据库连接db=pymysql.connect(host="数据库域名/ip",user="账号",password="密码",db="库名",port=3306)print(db)#使用 cursor() 方法创建一个游标对象 cursorcursor = db.cursor()print(cursor)#使用 execute() 方法执行 SQL 查询cursor.execute("select * from user where mobile")# 使用 fetchone() 方法.results1= cursor.fetchone()#获取单条(第1条)数据results2=cursor.fetchmany(3)#获取3条(2、3、4)数据results3=cursor.fetchall() #获取全部(5-全部)数据print(results3)#关闭游标,又从起始位置开始cursor.close()# 关闭数据库连接db.close()C:\Users\wangli\PyCharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py<pymysql.connections.Connection object at 0x00000161DDC90E80><pymysql.cursors.Cursor object at 0x00000161DF8E44E0>数据省略Process finished with exit code 0
#!/usr/bin/python3 import pymysql # 打开数据库连接db = pymysql.connect("localhost","testuser","test123","TESTDB" ) # 使用 cursor() 方法创建一个游标对象 cursorcursor = db.cursor() # 使用 execute() 方法执行 SQL,如果表存在则删除cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") # 使用预处理语句创建表sql = """CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )""" cursor.execute(sql) # 关闭数据库连接db.close()
import pymysql#创建数据库连接db=pymysql.connect(host="",user="",password="",db="ck",port=3306)print(db)#使用 cursor() 方法创建一个游标对象 cursorcursor = db.cursor()print(cursor)sql="INSERT INTO j.store (id,company_id,title,address,district,lon,lat,remark,updated_at,created_at,deleted_at) VALUES ('195', '61', '虹桥路店', '虹桥路1027号', '', '0.000000', '0.000000', NULL, '2019-03-15 14:54:29', '2019-03-15 14:54:29', NULL)"#写法2#cursor.execute('insert into stu(id,name,age) values(%s,%s,%s)',('201611001','xiaoqian',20))#插入多条数据,具体数据用列表来保存,列表元素是元组#cur.executemany('insert into stu(id,name,age) values(%s,%s,%s)',[('201611001','xiaoqian',20),('201611002','smile',21),('201611003','wood',23)])try: #执行插入sql cursor.execute(sql) # 提交到数据库执行 db.commit()except: # 如果发生错误则回滚 db.rollback()# 关闭数据库连接db.close()C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py<pymysql.connections.Connection object at 0x00000295DEA90EB8><pymysql.cursors.Cursor object at 0x00000295E06B44E0>Process finished with exit code 0
Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。
单条sql语句查询
sql = “SELECT * FROM EMPLOYEE WHERE INCOME > %s” % (1000)
多条sql语句查询
import pymysql #创建数据库连接 db=pymysql.connect(host="",user="",password="",db="",port=3306) print(db) #使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() print(cursor) sql="select * from c.user limit 10" try: #执行sql语句 cursor.execute(sql) #查询所有记录 results=cursor.fetchall() print(results)#打印10条数据 for row in results: print(row)#循环打印每条数据 user_id=row[0]#第1列字段 mobile=row[1]#第2列字段 print(user_id,mobile)#循环打印第1、2列字段 except: print('查询失败') #关闭数据库连接 db.close() C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py <pymysql.connections.Connection object at 0x0000018E8BEDB748> <pymysql.cursors.Cursor object at 0x0000018E8DB34550> ((2, '13888888888', '新', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554291602, 0, 1, 0, datetime.datetime(2019, 4, 3, 19, 37, 21), datetime.datetime(2019, 4, 3, 19, 49, 19), None), (75, '13888888888', '小', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554291602, 0, 1, 0, datetime.datetime(2019, 4, 3, 19, 37, 21), datetime.datetime(2019, 4, 3, 19, 49, 19), None), (139, '13888888888', '林', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554088503, 0, 2, 0, datetime.datetime(2019, 4, 1, 11, 11, 37), datetime.datetime(2019, 4, 1, 11, 15, 3), None), (190, '13888888888', '大', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554086702, 0, 1, 0, datetime.datetime(2019, 4, 1, 10, 40, 28), datetime.datetime(2019, 4, 1, 10, 45, 2), None), (192, '13888888888', '磊', '33010019900530156x', 0, '', '', '', 1, 1, '', '', '', '', '', 30, 13888888888, 0, 2, 0, datetime.datetime(2019, 3, 29, 17, 26, 56), datetime.datetime(2019, 4, 3, 19, 49, 19), None), (199, '13888888888', '一', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554086702, 0, 2, 0, datetime.datetime(2019, 4, 1, 10, 40, 28), datetime.datetime(2019, 4, 1, 16, 8, 3), None), (202, '13888888888', '三', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554086702, 0, 1, 0, datetime.datetime(2019, 4, 1, 10, 40, 29), datetime.datetime(2019, 4, 1, 10, 45, 2), None), (203, '13888888888', '四', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554086702, 0, 1, 0, datetime.datetime(2019, 4, 1, 10, 40, 29), datetime.datetime(2019, 4, 1, 10, 45, 2), None), (230, '13888888888', '大', '320382199303242813', 0, '', '', '', 1, 1, '', '', '', '', '', 30, 1554291602, 0, 2, 0, datetime.datetime(2019, 4, 3, 19, 37, 20), datetime.datetime(2019, 4, 3, 20, 11, 18), None), (278, '13700000003', '三', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554171902, 0, 2, 0, datetime.datetime(2019, 4, 2, 10, 24, 35), datetime.datetime(2019, 4, 2, 10, 25, 8), None)) (2, '13888888888', '林', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554291602, 0, 1, 0, datetime.datetime(2019, 4, 3, 19, 37, 21), datetime.datetime(2019, 4, 3, 19, 49, 19), None) 2 13888888888 (75, '13888888888', '马', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554291602, 0, 1, 0, datetime.datetime(2019, 4, 3, 19, 37, 21), datetime.datetime(2019, 4, 3, 19, 49, 19), None) 75 18817893609 (139, '13888888888', '林', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554088503, 0, 2, 0, datetime.datetime(2019, 4, 1, 11, 11, 37), datetime.datetime(2019, 4, 1, 11, 15, 3), None) 139 13888888888 (190, '13888888888', '徐', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554086702, 0, 1, 0, datetime.datetime(2019, 4, 1, 10, 40, 28), datetime.datetime(2019, 4, 1, 10, 45, 2), None) 190 13888888888 (192, '13888888888', '王', '330', 0, '', '', '', 1, 1, '', '', '', '', '', 30, 1553851802, 0, 2, 0, datetime.datetime(2019, 3, 29, 17, 26, 56), datetime.datetime(2019, 4, 3, 19, 49, 19), None) 192 13888888888 (199, '13888888888', '李', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554086702, 0, 2, 0, datetime.datetime(2019, 4, 1, 10, 40, 28), datetime.datetime(2019, 4, 1, 16, 8, 3), None) 199 13888888888 (202, '13888888888', '三', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554086702, 0, 1, 0, datetime.datetime(2019, 4, 1, 10, 40, 29), datetime.datetime(2019, 4, 1, 10, 45, 2), None) 202 13888888888 (203, '13888888888', '四', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554086702, 0, 1, 0, datetime.datetime(2019, 4, 1, 10, 40, 29), datetime.datetime(2019, 4, 1, 10, 45, 2), None) 203 13888888888 (230, '13888888888', '小', '320', 0, '', '', '', 1, 1, '', '', '', '', '', 30, 1554291602, 0, 2, 0, datetime.datetime(2019, 4, 3, 19, 37, 20), datetime.datetime(2019, 4, 3, 20, 11, 18), None) 230 13888888888 (278, '13700000003', '王', '', 0, '', '', '', 0, 1, '', '', '', '', '', 30, 1554171902, 0, 2, 0, datetime.datetime(2019, 4, 2, 10, 24, 35), datetime.datetime(2019, 4, 2, 10, 25, 8), None) 278 13700000003 Process finished with exit code 0
sql语句
sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')sql="update c.user set sex=2 where mobile=18221124104"cur.execute('update stu set age = %s where id = %s',(25,'201611006'))
import pymysql#创建数据库连接conn=pymysql.connect(host="",user="qa",password="Qa",db="c",port=3306)print(conn)#使用 cursor() 方法创建一个游标对象 cursorcursor = conn.cursor()print(cursor)sql="update c.user set sex=2 where mobile=18221124104"try: cursor.execute(sql) conn.commit()except: conn.rollback()conn.close()C:\Users\wangli\PycharmProjects\AutoMation\venv\Scripts\python.exe C:/Users/wangli/PycharmProjects/AutoMation/case/test.py<pymysql.connections.Connection object at 0x000002C281555A20><pymysql.cursors.Cursor object at 0x000002C2831944A8>Process finished with exit code 0
#!/usr/bin/python3 import pymysql # 打开数据库连接db = pymysql.connect("localhost","testuser","test123","TESTDB" ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL 删除语句sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (20)try: # 执行SQL语句 cursor.execute(sql) # 提交修改 db.commit()except: # 发生错误时回滚 db.rollback() # 关闭连接db.close()
如果对Python感兴趣的话,可以试试我整理的这份Python全套学习资料,文末免费领取
包括:Python永久使用安装包、Python web开发,python爬虫,Python数据分析,人工智能、机器学习等学习教程。带你从零基础系统性的学好Python!
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
工欲善其事,必先利其器。学习Python常用的开发软件都在这里了!
还有很多适合0基础入门的学习视频,有了这些视频,轻轻松松上手Python~
每节视频课后,都有对应的练习题哦,可以检验学习成果哈哈!
光学理论是没用的,要学会跟着一起敲代码,动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。这份资料也包含在内的哈~
我们学会了Python之后,有了技能就可以出去找工作啦!下面这些面试题是都来自阿里、腾讯、字节等一线互联网大厂,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
上述完整版Python全套学习资料已经上传CSDN官方,需要的小伙伴可自行微信扫描下方CSDN官方认证二维码输入“领取资料”免费领取!!
来源地址:https://blog.csdn.net/Java_ZZZZZ/article/details/130822606
--结束END--
本文标题: Python之Pymysql模块操作MySQL增删改查
本文链接: https://lsjlt.com/news/407640.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0