Python操作MySQL之pymsql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。
下载安装
pip3 install pymysql
使用操作
1、执行SQL
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1',charset='utf8') # 创建游标 cursor = conn.cursor() # 执行SQL,并返回收影响行数 effect_row = cursor.execute("update hosts set host = '1.1.1.2'") # 执行SQL,并返回受影响行数 #effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,)) # 执行SQL,并返回受影响行数 #effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)]) # 提交,不然无法保存新建或者修改的数据 conn.commit() # 关闭游标 cursor.close() # 关闭连接 conn.close()
2、获取新创建数据自增ID
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') cursor = conn.cursor() cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)]) conn.commit() cursor.close() conn.close() # 获取最新自增ID new_id = cursor.lastrowid
3、获取查询数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') cursor = conn.cursor() cursor.execute("select * from hosts") # 获取第一行数据 row_1 = cursor.fetchone() # 获取前n行数据 # row_2 = cursor.fetchmany(3) # 获取所有数据 # row_3 = cursor.fetchall() conn.commit() cursor.close() conn.close()
注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:
- cursor.scroll(1,mode='relative') # 相对当前位置移动
- cursor.scroll(2,mode='absolute') # 相对绝对位置移动
4、fetch数据类型
关于默认获取的数据是元祖类型,如果想要或者字典类型的数据,即:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') # 游标设置为字典类型 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) r = cursor.execute("call p1()") result = cursor.fetchone() conn.commit() cursor.close() conn.close()
5、总结
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# author:wanstack import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1',user='root',password='root1234',port=3306,database='test',charset='utf8') # 创建游标 cursor = conn.cursor() # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 增 # effect_row = cursor.execute("insert into class(caption) values('全站二班')") # print(effect_row) # effect_row = cursor.executemany("insert into class(caption) values(%s)",(('python1'),('python2'),('python3'))) # print(effect_row) # 获取最新自增ID # new_id = cursor.lastrowid # print(new_id) # 删 # effect_row = cursor.execute("delete from class where caption='python1'") # print(effect_row) # 改 # effect_row = cursor.execute("update class set caption='Linux' where cid=10") # print(effect_row) # # conn.commit() # =============== 以上操作均需要conn.commit() 提交之后才会生效,查询不需要提交 # 查 effect_row = cursor.execute('select * from student') # print(effect_row) # row_1 = cursor.fetchone() # print(row_1) # row_many = cursor.fetchmany(3) # print(row_many) # row_all = cursor.fetchall() # print(row_all) """ 注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如: cursor.scroll(1,mode='relative') # 相对当前位置移动,1表示往下移动一行,-1表示往上移动一行 cursor.scroll(2,mode='absolute') # 相对绝对位置移动 """ row_many1 = cursor.fetchmany(3) print(row_many1) # cursor.scroll(1,mode='relative') # cursor.scroll(-2,mode='relative') cursor.scroll(1,mode='absolute') row_many2 = cursor.fetchmany(3) print(row_many2) cursor.close() conn.close()
生活不会突变,你要做的只是耐心和积累。人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。你的时间有限,做喜欢的事情会令人愉悦,所以跟随自己的本心。