欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python3連接mysql實(shí)際操作

下文給大家?guī)黻P(guān)于python3連接MySQL實(shí)際操作,感興趣的話就一起來看看python3連接mysql實(shí)際操作這篇文章吧,相信看完對(duì)大家多少有點(diǎn)幫助吧。

調(diào)兵山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),調(diào)兵山網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為調(diào)兵山上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的調(diào)兵山做網(wǎng)站的公司定做!

python3 連接mysql數(shù)據(jù)庫,執(zhí)行操作。

環(huán)境:
os: windows 2008
python: python 3.5.3

之前用過python3連接sqlite3數(shù)據(jù)庫,只是作為單機(jī)數(shù)據(jù)庫使用,但后來提供web服務(wù)時(shí),sqlite3數(shù)據(jù)庫支持的不夠好,轉(zhuǎn)而使用mysql數(shù)據(jù)庫。

python3連接數(shù)據(jù)庫使用pymysql模塊。

下面總結(jié)一下,寫成類方便使用:

class ConMysql:

    def __init__(self, host, username, password, database):
                self._database = database
                self._host = host
                self._user = username
                self._passwd = password

        def connect(self):
            """連接數(shù)據(jù)庫,執(zhí)行SQL語句,返回元組"""

                #連接數(shù)據(jù)庫
                try:
                    self._db = pymysql.connect(self._host, self._user, self._passwd, self._database)
                except (ConnectionRefusedError, pymysql.err.OperationalError, pymysql.err.InternalError) as _con_err:
                    return False, _con_err
                else:
                        return True, 'OK'

        def get_data(self, _sql_str, s='r'):
                # 查詢
                _cur = self._db.cursor()
                try:
                        _cur.execute(_sql_str)
                except (pymysql.err.InternalError, pymysql.err.OperationalError, pymysql.err.ProgrammingError) as _sql_err:
                        _cur.close()
                        return False, _sql_err
                if s == 'r'
                        _cur.close()
                        _array = _cur.fetchall()
                        return True, _array
                else:
                        _cur.close()
                        self._db.commit()
                        return True, 'OK'

        def edit_data(self, _sql_str):
                # 修改
                return self.get_data(_sql_str, 'w')

        def __del__(self):
                self._db.close()

這里讀數(shù)據(jù)庫沒有問題,在插入或修改時(shí)會(huì)報(bào)錯(cuò),因?yàn)榫幋a的問題,pymysql默認(rèn)會(huì)把所以的sql字符串編譯成byte,為了避免錯(cuò)誤可以添加兩個(gè)參數(shù),use_unicode=True, charset='utf8'。其實(shí)在實(shí)例化時(shí)還有很多可選參數(shù),詳細(xì)的可以查看文檔或你已經(jīng)安裝的源代碼connection。

看了以上關(guān)于python3連接mysql實(shí)際操作詳細(xì)內(nèi)容,是否有所收獲。如果想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。

新聞名稱:python3連接mysql實(shí)際操作
文章轉(zhuǎn)載:http://chinadenli.net/article10/giepdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航、自適應(yīng)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)