pymsql是Python中操做MySQL的模块,其使用方法和MySQLdb几乎相同。python
pip3 install pymysql
执行SQL语句的基本语法:mysql
须要注意的是:建立链接后,都由游标来进行与数据库的操做,于是获取数据也须要游标。sql
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'wyf' import pymysql #建立连接 conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='123456',db='hive_test') #建立游标 cursor = conn.cursor() #print cursor #执行语句,返回数据结果总数量 effect_new = cursor.execute('select * from A') #print effect_new #结果是 7 # 执行SQL,并返回受影响行数 effect_row = cursor.executemany('select * from A where aID=%s',[3,4,5]) print effect_row #结果是3 # 提交,否则没法保存新建或者修改的数据 conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
能够获取到最新自增的ID,也就是最后插入的一条数据ID数据库
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'wyf' import pymysql #建立连接 conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='123456',db='hive_test') #建立游标 cursor = conn.cursor() #print cursor # 执行SQL,并返回受影响行数 effect_row = cursor.executemany('insert into A(aNum) values(%s)',['ggg','hhhh','ffff']) # 提交,否则没法保存新建或者修改的数据 conn.commit() print cursor.fetchall() # 关闭游标 cursor.close() # 关闭链接 conn.close() # 获取最新自增ID,注意是最新的id,多条也是一条 new_id = cursor.lastrowid print new_id
获取查询数据的三种方式:获取第一行数据,获取前n行数据,获取全部数据 (数据是以元祖的方式存放)函数
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'wyf' import pymysql #建立连接 conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='123456',db='hive_test') #建立游标 cursor = conn.cursor() #print cursor #获取全部数据 cursor.execute('select * from A') #获取第一条数据 row_1 = cursor.fetchone() print 'row_1=',row_1 #获取前N条数据 row_n = cursor.fetchmany(3) print 'row_n=',row_n # 获取全部数据 row_all = cursor.fetchall() print 'row_all=',row_all conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
结果:fetch
注:在fetch数据时按照顺序进行,能够使用cursor.scroll(num,mode)来移动游标位置,如:spa
示例:指针
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'wyf' import pymysql #建立连接 conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='123456',db='hive_test') #建立游标 cursor = conn.cursor() #print cursor #获取全部数据 cursor.execute('select * from A') #全部查询数据都打印一编,方便对比 print '''row_all= ((1, 'a20050111'), (9, 'sdasdas'), (3, 'a20050113'), (4, 'a20050114'), (5, 'a20050115'), (6, 'a20160928'), (7, 'sdasdasdadasd'), (8, 'sdasdasdadasd'), (17, 'ffff'), (16, 'hhhh'), (15, 'ggg'))''' cursor.scroll(1, mode='relative') # 相对当前位置移动 row_all_1 = cursor.fetchone() print 'row_all_1=',row_all_1 cursor.scroll(2, mode='absolute') # 相对绝对位置移动 row_all_2 = cursor.fetchone() print 'row_all_2=',row_all_2 conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
结果:code
总结:blog
1.当用fetchall的时候游标指针会移动到最后,请在使用scroll的时候注意。
2.结果分析,相对是当前指针的位置,绝对是指针总体位置。
3.(1, mode='relative') 当前指针下移一个的位置,及地第二个,(2, mode='absolute'),最初的指针下移两个,由于这个结果指针最初都是最开始。
默认拿到的数据是元祖类型,若是是字典的话会更方便操做,那方法来了:
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'wyf' import pymysql #建立连接 conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='123456',db='hive_test') #建立游标 cursor = conn.cursor() #print cursor # 游标设置为字典类型 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) r = cursor.execute("select * from A") result = cursor.fetchone() print 'result=',result conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
# 利用with定义函数 @contextlib.contextmanager def mysql(self, host='192.168.14.88',port=3306,user='root',passwd='123456',db='hive_test', charset='utf8'): self.conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db, charset=charset) self.cuersor = self.conn.cursor(cursor=pymysql.cursors.DictCursor) try: yield self.cuersor finally: self.conn.commit() self.cuersor.close() self.conn.close() # 执行 with mysql() as cuersor: print(cuersor) # 操做MySQL代码块