真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Python基于pymysql的數(shù)據(jù)庫操作類的安裝運(yùn)行過程

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Python基于pyMySQL的數(shù)據(jù)庫操作類的安裝運(yùn)行過程,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)建站是專業(yè)的鹽池網(wǎng)站建設(shè)公司,鹽池接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行鹽池網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

一 簡介
     Python和MySQL交互的模塊有 MySQLdb 和 PyMySQL(pymysql),MySQLdb是基于C 語言編寫的,而且Python3 不在支持MySQLdb 。PyMySQL是一個(gè)純Python寫的MySQL客戶端,它的目標(biāo)是替代MySQLdb,可以在CPython、PyPy、IronPython和Jython環(huán)境下運(yùn)行,PyMySQL在MIT許可下發(fā)布。
    在開發(fā)基于Python語言的項(xiàng)目中,為了以后系統(tǒng)能兼容Python3,我們使用了PyMySQL替換了MySQLdb。下面我們來熟悉一下pymysql的使用。
 
二 安裝方式
  pymsql的源碼 https://github.com/PyMySQL/PyMySQL ,目前還在持續(xù)更新。

  1. 安裝要求:

  2. Python -- one of the following:

  3.      CPython >= 2.6 or >= 3.3

  4.      PyPy >= 4.0

  5.      IronPython 2.7

  6. MySQL Server -- one of the following:

  7.      MySQL >= 4.1 (tested with only 5.5~)

  8.      MariaDB >= 5.1

  9. 安裝

  10.   pip install PyMySQL

 

三 基于pymysql的數(shù)據(jù)庫交互

  1. #!/usr/bin/env python

  2. # encoding: utf-8

  3. """

  4. author: yangyi@youzan

  5. time:   2015/6/8 上午11:34

  6. func: 基于pymysql的數(shù)據(jù)庫交互類,支持事務(wù)提交和回滾,返回結(jié)果記錄行數(shù),和insert的最新id

  7. """

  8. import pymysql

  9. from warnings import filterwarnings

  10. filterwarnings('ignore', category=pymysql.Warning)

  11. CONNECT_TIMEOUT = 100

  12. IP = 'localhost'

  13. PORT = 3306

  14. USER = 'root'

  15. PASSSWORD = ''

  16. class QueryException(Exception):

  17.     """

  18.     """

  19. class ConnectionException(Exception):

  20.     """

  21.     """

  22. class MySQL_Utils():

  23.     def __init__(

  24.             self, ip=IP, port=PORT, user=USER, password=PASSSWORD,

  25.             connect_timeout=CONNECT_TIMEOUT, remote=False, socket='', dbname='test'):

  26.         self.__conn = None

  27.         self.__cursor = None

  28.         self.lastrowid = None

  29.         self.connect_timeout = connect_timeout

  30.         self.ip = ip

  31.         self.port = port

  32.         self.user = user

  33.         self.password = password

  34.         self.mysocket = socket

  35.         self.remote = remote

  36.         self.db = dbname

  37.         self.rows_affected = 0

  38.     def __init_conn(self):

  39.         try:

  40.             conn = pymysql.connect(

  41.                     host=self.ip,

  42.                     port=int(self.port),

  43.                     user=self.user,

  44.                     db=self.db,

  45.                     connect_timeout=self.connect_timeout,

  46.                     charset='utf8', unix_socket=self.mysocket)

  47.         except pymysql.Error as e:

  48.             raise ConnectionException(e)

  49.         self.__conn = conn

  50.     def __init_cursor(self):

  51.         if self.__conn:

  52.             self.__cursor = self.__conn.cursor(pymysql.cursors.DictCursor)

  53.     def close(self):

  54.         if self.__conn:

  55.             self.__conn.close()

  56.             self.__conn = None

  57.     #專門處理select 語句

  58.     def exec_sql(self, sql, args=None):

  59.         try:

  60.             if self.__conn is None:

  61.                 self.__init_conn()

  62.                 self.__init_cursor()

  63.             self.__conn.autocommit = True

  64.             self.__cursor.execute(sql, args)

  65.             self.rows_affected = self.__cursor.rowcount

  66.             results = self.__cursor.fetchall()

  67.             return results

  68.         except pymysql.Error as e:

  69.             raise pymysql.Error(e)

  70.         finally:

  71.             if self.__conn:

  72.                 self.close()

  73.     # 專門處理dml語句 delete,updete,insert 

  74.     def exec_txsql(self, sql, args=None):

  75.         try:

  76.             if self.__conn is None:

  77.                 self.__init_conn()

  78.                 self.__init_cursor()

  79.             if self.__cursor is None:

  80.                 self.__init_cursor()

  81.             self.rows_affected=self.__cursor.execute(sql, args)

  82.             self.lastrowid = self.__cursor.lastrowid

  83.             return self.rows_affected

  84.         except pymysql.Error as e:

  85.             raise pymysql.Error(e)

  86.         finally:

  87.             if self.__cursor:

  88.                 self.__cursor.close()

  89.                 self.__cursor = None

  90.     # 提交

  91.     def commit(self):

  92.         try:

  93.             if self.__conn:

  94.                 self.__conn.commit()

  95.         except pymysql.Error as e:

  96.             raise pymysql.Error(e)

  97.         finally:

  98.             if self.__conn:

  99.                 self.close()

  100.     #回滾操作

  101.     def rollback(self):

  102.         try:

  103.             if self.__conn:

  104.                 self.__conn.rollback()

  105.         except pymysql.Error as e:

  106.             raise pymysql.Error(e)

  107.         finally:

  108.             if self.__conn:

  109.                 self.close()

  110.     # 適用于需要獲取插入記錄的主鍵自增id

  111.     def get_lastrowid(self):

  112.         return self.lastrowid

     #獲取dml操作影響的行數(shù)
    def get_affectrows(self):
        return self.rows_affected
     #MySQL_Utils初始化的實(shí)例銷毀之后,自動(dòng)提交
     def __del__(self):
        self.commit()

上述就是小編為大家分享的Python基于pymysql的數(shù)據(jù)庫操作類的安裝運(yùn)行過程了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


新聞名稱:Python基于pymysql的數(shù)據(jù)庫操作類的安裝運(yùn)行過程
文章起源:http://weahome.cn/article/igjsss.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部