首頁  >  文章  >  資料庫  >  MySQL適配器之PyMySQL的詳細介紹

MySQL適配器之PyMySQL的詳細介紹

黄舟
黄舟原創
2017-09-20 11:10:121512瀏覽

這篇文章主要為大家詳細介紹了MySQL適配器PyMySQL的相關資料,具有一定的參考價值,有興趣的小夥伴們可以參考一下

本文我們為大家介紹Python3 使用PyMySQL 連接資料庫,並實現簡單的增刪改查。

什麼是 PyMySQL?

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

PyMySQL 遵循 Python 資料庫 API v2.0 規範,並包含了 pure-Python MySQL 用戶端程式庫。

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

PyMySQL 下載

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


$ pip install PyMySQL

如果你的系統不支援pip 指令,可以使用以下方式安裝:

1、使用git 指令下載安裝套件安裝(你也可以手動下載):


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

2、資料庫操作實例,直接上程式碼。


import pymysql
import datainfo
import time

#获取参数

host = datainfo.host
username = datainfo.username
password = datainfo.password
database = datainfo.db

print()

#测试数据库连接
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 <&#39;%d&#39; " %(1)
 try:
  #执行sql
  print("执行删除")
  cursor.execute(sql)

  db.commit()
 except UnicodeEncodeError as e :
  #发生错误时回滚
  print(e)
  db.rollback()
 db.close()


if __name__ == &#39;__main__&#39;:
 testconnect()
 InsertDate()
 selectData()
 update_data()
 delete_Date()

以上是MySQL適配器之PyMySQL的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn