標籤:資料庫 適配器 demo sql 插入 刪除 使用 info 復原
本文我們為大家介紹 Python3 使用 PyMySQL 串連資料庫,並實現簡單的增刪改查。
什麼是 PyMySQL?
PyMySQL 是在 Python3.x 版本中用於串連 MySQL 伺服器的一個庫,Python2中則使用mysqldb。
PyMySQL 遵循 Python 資料庫 API v2.0 規範,並包含了 pure-Python MySQL 用戶端庫。
PyMySQL 安裝
在使用 PyMySQL 之前,我們需要確保 PyMySQL 已安裝。
PyMySQL :https://github.com/PyMySQL/PyMySQL。
如果還未安裝,我們可以使用以下命令安裝最新版的 PyMySQL:
$ pip install PyMySQL
如果你的系統不支援 pip 命令,可以使用以下方式安裝:
1、使用 git 命令下載安裝包安裝(你也可以手動下載):
$ git clone https://github.com/PyMySQL/PyMySQL$ cd PyMySQL/$ python3 setup.py install
2、資料庫操作執行個體,直接上代碼。
import pymysqlimport datainfoimport time#擷取參數host = datainfo.hostusername = datainfo.usernamepassword = datainfo.passworddatabase = datainfo.dbprint()#測試資料庫連接def testconnect(): #開啟資料庫連結 db = pymysql.connect(host,username,password,database) #使用cursor() 方法建立一個遊標對象 cursor cursor = db.cursor() #使用execute()方法執行SQL查詢 cursor.execute("select version()") #使用fetchone ()擷取單條資料 data = cursor.fetchone() print(data) db.close()#插入資料庫def InsertDate(): #開啟資料庫連結 db = pymysql.connect(host,username,password,database,charset=‘utf8‘) #使用cursor() 方法建立一個遊標對象 cursor cursor = db.cursor() create_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘) update_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘) start_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘) end_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘) remark = "測試插入資訊" print("開始") #Sql 插入語句 sql = "insert into demo(start_time,end_time,creat_time,update_time,remark) " "VALUES (‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘)" %(start_time,end_time,create_time,update_time,remark) try: #執行sql print("執行插入") tt = cursor.execute(sql) print(tt) db.commit() except UnicodeEncodeError as e : #發生錯誤時復原 print(e) db.rollback() db.close()#查詢操作def selectData(): db = pymysql.connect(host, username, password, database, charset=‘utf8‘) # 使用cursor() 方法建立一個遊標對象 cursor cursor = db.cursor() sql = "select * from demo where id >=‘%d‘" %(1) try: #執行sql print("執行查詢") cursor.execute(sql) results = cursor.fetchall() for row in results: id = row[0] start_time = row[1] end_time = row[2] create_time = row[3] update_time = row[4] remark = row[5] #列印結果 print("id = %d,start_time=%s,end_time=%s,create_time=%s,update_time=%s,remark=%s" %(id,start_time,end_time,create_time,update_time,remark)) db.commit() except UnicodeEncodeError as e : #發生錯誤時復原 print(e) db.close()#更新操作def update_data(): db = pymysql.connect(host, username, password, database, charset=‘utf8‘) # 使用cursor() 方法建立一個遊標對象 cursor cursor = db.cursor() update_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘) sql = "update demo set update_time =‘%s‘ where id >=‘%d‘ " %(update_time,1) try: #執行sql print("執行更新") cursor.execute(sql) db.commit() except UnicodeEncodeError as e : #發生錯誤時復原 print(e) db.rollback() db.close()#刪除操作def delete_Date(): db = pymysql.connect(host, username, password, database, charset=‘utf8‘) # 使用cursor() 方法建立一個遊標對象 cursor cursor = db.cursor() sql = "delete from demo where id <‘%d‘ " %(1) try: #執行sql print("執行刪除") cursor.execute(sql) db.commit() except UnicodeEncodeError as e : #發生錯誤時復原 print(e) db.rollback() db.close()if __name__ == ‘__main__‘: testconnect() InsertDate() selectData() update_data() delete_Date()
Python3 - MySQL適配器 PyMySQL