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代碼塊