国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

MySQL適配器PyMySQL詳解

2020-01-04 16:46:19
字體:
來源:轉載
供稿:網友

本文我們?yōu)榇蠹医榻B Python3 使用 PyMySQL 連接數(shù)據(jù)庫,并實現(xiàn)簡單的增刪改查。

什么是 PyMySQL?

PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務器的一個庫,Python2中則使用mysqldb。

PyMySQL 遵循 Python 數(shù)據(jù)庫 API v2.0 規(guī)范,并包含了 pure-Python MySQL 客戶端庫。

PyMySQL 安裝
在使用 PyMySQL 之前,我們需要確保 PyMySQL 已安裝。

PyMySQL 下載地址:https://github.com/PyMySQL/PyMySQL。

如果還未安裝,我們可以使用以下命令安裝最新版的 PyMySQL:

$ pip install PyMySQL

如果你的系統(tǒng)不支持 pip 命令,可以使用以下方式安裝:

1、使用 git 命令下載安裝包安裝(你也可以手動下載):

$ git clone https://github.com/PyMySQL/PyMySQL$ cd PyMySQL/$ python3 setup.py install

2、數(shù)據(jù)庫操作實例,直接上代碼。

 

import pymysqlimport datainfoimport time#獲取參數(shù)host = datainfo.hostusername = datainfo.usernamepassword = datainfo.passworddatabase = datainfo.dbprint()#測試數(shù)據(jù)庫連接def testconnect(): #打開數(shù)據(jù)庫鏈接 db = pymysql.connect(host,username,password,database) #使用cursor() 方法創(chuàng)建一個游標對象 cursor cursor = db.cursor() #使用execute()方法執(zhí)行SQL查詢 cursor.execute("select version()") #使用fetchone ()獲取單條數(shù)據(jù) data = cursor.fetchone() print(data) db.close()#插入數(shù)據(jù)庫def InsertDate(): #打開數(shù)據(jù)庫鏈接 db = pymysql.connect(host,username,password,database,charset='utf8') #使用cursor() 方法創(chuàng)建一個游標對象 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:  #執(zhí)行sql  print("執(zhí)行插入")  tt = cursor.execute(sql)  print(tt)  db.commit() except UnicodeEncodeError as e :  #發(fā)生錯誤時回滾  print(e)  db.rollback() db.close()#查詢操作def selectData(): db = pymysql.connect(host, username, password, database, charset='utf8') # 使用cursor() 方法創(chuàng)建一個游標對象 cursor cursor = db.cursor() sql = "select * from demo where id >='%d'" %(1) try:  #執(zhí)行sql  print("執(zhí)行查詢")  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 :  #發(fā)生錯誤時回滾  print(e) db.close()#更新操作def update_data(): db = pymysql.connect(host, username, password, database, charset='utf8') # 使用cursor() 方法創(chuàng)建一個游標對象 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:  #執(zhí)行sql  print("執(zhí)行更新")  cursor.execute(sql)  db.commit() except UnicodeEncodeError as e :  #發(fā)生錯誤時回滾  print(e)  db.rollback() db.close()#刪除操作def delete_Date(): db = pymysql.connect(host, username, password, database, charset='utf8') # 使用cursor() 方法創(chuàng)建一個游標對象 cursor cursor = db.cursor() sql = "delete from demo where id <'%d' " %(1) try:  #執(zhí)行sql  print("執(zhí)行刪除")  cursor.execute(sql)  db.commit() except UnicodeEncodeError as e :  #發(fā)生錯誤時回滾  print(e)  db.rollback() db.close()if __name__ == '__main__': testconnect() InsertDate() selectData() update_data() delete_Date()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 当阳市| 河北区| 太原市| 大厂| 长垣县| 邢台市| 平原县| 施甸县| 翁源县| 张家港市| 新和县| 枣庄市| 江城| 古蔺县| 银川市| 扶余县| 台东市| 黄梅县| 柳州市| 镇坪县| 克拉玛依市| 固镇县| 罗平县| 康保县| 五指山市| 灌南县| 纳雍县| 林芝县| 孟州市| 钟山县| 秦安县| 城固县| 遂昌县| 稷山县| 当雄县| 勃利县| 贵州省| 祁东县| 荥经县| 宜丰县| 即墨市|