PyMySQL 是在 Python3.x 版本中用於鏈接 MySQL 服務器的一個庫,Python2中則使用mysqldb。python
PYmysql安裝mysql
pip install pymysql
建立鏈接sql
# 導入pymysql模塊 import pymysql # 鏈接database conn = pymysql.connect(host=「你的數據庫地址」, user=「用戶名」,password=「密碼」,database=「數據庫名」,charset=「utf8」) # 獲得一個能夠執行SQL語句的光標對象 cursor = conn.cursor() # 定義要執行的SQL語句 sql = """ CREATE TABLE USER1 ( id INT auto_increment PRIMARY KEY , name CHAR(10) NOT NULL UNIQUE, age TINYINT NOT NULL )ENGINE=innodb DEFAULT CHARSET=utf8; """ # 執行SQL語句 cursor.execute(sql) # 關閉光標對象 cursor.close() # 關閉數據庫鏈接 conn.close() 複製代碼
返回字典格式數據:數據庫
# 導入pymysql模塊 import pymysql # 鏈接database conn = pymysql.connect(host=「你的數據庫地址」, user=「用戶名」,password=「密碼」,database=「數據庫名」,charset=「utf8」) # 獲得一個能夠執行SQL語句而且將結果做爲字典返回的遊標 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 定義要執行的SQL語句 sql = """ CREATE TABLE USER1 ( id INT auto_increment PRIMARY KEY , name CHAR(10) NOT NULL UNIQUE, age TINYINT NOT NULL )ENGINE=innodb DEFAULT CHARSET=utf8; """ # 執行SQL語句 cursor.execute(sql) # 關閉光標對象 cursor.close() # 關閉數據庫鏈接 conn.close() 複製代碼
簡單驗證功能api
import pymysql name =input('請輸入帳號'); pwd =input('請輸入密碼') conn =pymysql.connect(host ='127.0.0.1',port=3306, user = 'root',password ='123456',database ='day59') cursor =conn.cursor() #獲取一個光標,等等輸入sql語句 sql ='select * from userinfo where name =%s and pwd =%s ;' #幫我拼接字符串的sql語句,而且去數據庫執行. ret =cursor.execute(sql,[name,pwd]) #關閉 cursor.close() conn.close() if ret: print('login successfully') else: print('login failed!')
注意 : 服務器
charset =‘utf8’ ,不要寫成utf-8fetch
增刪改查操做spa
1、增長數據code
1.一、經過python 爲mysql添加多條數據 對象
import pymysql conn = pymysql.connect( host ='127.0.0.1', port =3306, #不是字符串不須要加引號。 user ='root', password ='123456', database ='day59', charset ='utf8' ) #獲取一個光標 cursor =conn.cursor() #定義要執行的sql語句 sql ='insert into userinfo(name,pwd) values(%s,%s);' data =[ ('高材生','woyouqian'), ('changjiang','huanghe'), ('zhanshen','jichuchapiqida') ] #拼接並執行sql語句 cursor.executemany(sql,data) #涉及寫操做要注意提交 conn.commit() #關閉鏈接 cursor.close() conn.close()
output result:
1.二、插入單條數據
import pymysql conn =pymysql.connect( host ='127.0.0.1', port = 3306, user = 'root', password ='123456', database ='day59', charset ='utf8' ) cursor =conn.cursor() 獲取一個光標 sql =' insert into userinfo (name,pwd) values (%s,%s);' name = 'wangzhen' pwd = '123456' cursor.execute(sql, [name,pwd]) conn.commit() cursor.close() conn.close()
1.3 、獲取最新插入數據 (最後一條)
import pymysql # 創建鏈接 conn = pymysql.connect( host="127.0.0.1", port=3306, # 不是字符串類型(不要加引號) user="root", password="123456", database="day59", charset="utf8" # 沒有-!!!! ) # 獲取一個光標 cursor = conn.cursor() # 定義將要執行的SQL語句 sql = "insert into userinfo (name, pwd) values (%s, %s);" name = "wangzhen3號" pwd = "123456" # 拼接並執行SQL語句 cursor.execute(sql, [name, pwd]) # 涉及寫操做注意要提交 conn.commit() # 關閉鏈接 # 獲取最新的那一條數據的ID last_id = cursor.lastrowid print("最後一條數據的ID是:", last_id) cursor.close() conn.close()
輸出結果
2、刪除操做
import pymysql # 創建鏈接 conn = pymysql.connect( host="127.0.0.1", port=3306, # 不是字符串類型(不要加引號) user="root", password="123456", database="day59", charset="utf8" # 沒有-!!!! ) # 獲取一個光標 cursor = conn.cursor() # 定義將要執行的SQL語句 sql = "delete from userinfo where name=%s;" name = "高材生" # 拼接並執行SQL語句 cursor.execute(sql, [name]) # 涉及寫操做注意要提交 conn.commit() # 關閉鏈接 cursor.close() conn.close()
三 、更改數據
""" pymysql插入新數據 """ import pymysql # 創建鏈接 conn = pymysql.connect( host="127.0.0.1", port=3306, # 不是字符串類型(不要加引號) user="root", password="123456", database="day59", charset="utf8" # 沒有-!!!! ) # 獲取一個光標 cursor = conn.cursor() # 定義將要執行的SQL語句 sql = "update userinfo set pwd=%s where name=%s;" # 拼接並執行SQL語句 cursor.execute(sql, ["woshixinmima", "alex"]) # 涉及寫操做注意要提交 conn.commit() # 關閉鏈接 cursor.close() conn.close()
4、fetch數據(查詢數據)
import pymysql conn = pymysql.connect( host ='127.0.0.1', port =3306, #不是字符串不須要加引號。 user ='root', password ='123456', database ='day59', charset ='utf8' ) #獲取一個光標 # cursor = conn.cursor()# 默認返回元組數據類型 cursor = conn.cursor(cursor =pymysql.cursors.DictCursor)#返回字典數據類型 #定義將要執行的sql語句 sql = 'select name,pwd from userinfo;' #拼接並執行sql語句 cursor.execute(sql) #取到查詢結果 ret1 =cursor.fetchone() #取一條 ret2 =cursor.fetchmany(3)# 取三條 ret3 =cursor.fetchone() #取一條 cursor.close() conn.close() print(ret1) print(ret2) print(ret3)
# 能夠獲取指定數量的數據 cursor.fetchmany(3) # 光標按絕對位置移動1 cursor.scroll(1, mode="absolute") # 光標按照相對位置(當前位置)移動1 cursor.scroll(1, mode="relative")
輸出結果:
5、數據回滾
""" pymysql插入新數據 """ import pymysql # 創建鏈接 conn = pymysql.connect( host="127.0.0.1", port=3306, # 不是字符串類型(不要加引號) user="root", password="123456", database="day59", charset="utf8" # 沒有-!!!! ) # 獲取一個光標 cursor = conn.cursor() # 定義將要執行的SQL語句 sql1 = "insert into userinfo (name, pwd) values (%s, %s);" sql2 = "insert into book (name) values (%s);" name = "wangzhen2號" pwd = "123456" book_title = "Python從入門到放棄" try: # 拼接並執行SQL語句 cursor.execute(sql1, [name, pwd]) cursor.execute(sql2) # 報錯的SQL語句 # 涉及寫操做注意要提交 conn.commit() except Exception as e: print(str(e)) # 有異常就回滾 conn.rollback() # 關閉鏈接 cursor.close() conn.close()