window10系統下使用python3.3版本實現mysql查詢

來源:互聯網
上載者:User

標籤:

參考文檔:

兔大俠整理的MySQL-Python(MySQLdb)封裝類

Python安裝模組出錯(ImportError: No module named setuptools)解決方案

 

環境 windows10 | python3.3 

1.確保已經安裝setuptools方法如下:>下載安裝的指令碼https://bootstrap.pypa.io/ez_setup.py,下載該指令碼後運行     >python ez_setup.py     >即可。指令碼會自動判斷python的版本,自動下載,安裝。2.安裝pip方法如下:我們同樣需要在Python的官網上去下載,是:https://pypi.python.org/pypi/pip#downloads :解壓到某目錄下,cd進去,使用命令 python setup.py install 進行安裝將X:\Python\Script 目錄添加到path   3.安裝mysqldbpip install mysql-python  測試檔案:DB.py
#!/usr/bin/env pythonimport MySQLdbimport timeclass ZDB:        error_code = ‘‘    _instance = None    _conn = None    _cur = None    _TIMEOUT = 30    _timecount = 0    def __init__(self,dbconfig):        try:            self._conn = MySQLdb.connect(host=dbconfig[‘host‘],                                         port=dbconfig[‘port‘],                                         user=dbconfig[‘user‘],                                         passwd=dbconfig[‘passwd‘],                                         db=dbconfig[‘db‘],                                         charset=dbconfig[‘charset‘])        except MySQLdb.Error,e:            self.error_code = e.args[0]            error_msg = "MYSQL ERROR ! ",e.args[0].e.args[1]            print error_msg            if self._timecount < self._TIMEOUT:                interval = 5                self._timecount += interval                time.sleep(interval)                return self.__init__(dbconfig)            else:                raise Exception(error_msg)        self._cur = self._conn.cursor()        self._instance = MySQLdb    def query(self,sql):        try:            self._cur.execute("SET NAMES UTF8")            result = self._cur.execute(sql)        except MySQLdb.error,e:            self.error_code = e.args[0]            print "MYSQL ERROR-Query:",e.args[0],e.args[1]            result=FALSE        return result    def update(self,sql):        try:            self._cur.execute("SET NAMES UTF8")            result = self._cur.execute(sql)            self._conn.commit()        except MySQLdb.Error,e:            self.error_code = e.args[0]            print "MYSQL ERROR-Update:",e.args[0],e.args[1]            result=FALSE        return result    def insert(self,sql):        try:            self._cur.execute("SET NAMES UTF8")            self._cur.execute(sql)            self._conn.commit()            return self._conn.insert_id()        except MySQLdb.Error,e:            self.error_code = e.args[0]            print "MYSQL ERROR-Insert:",e.args[0],e.args[1]            result=FALSE    def fetchAllRows(self):        return self._cur.fetchall()    def getRowCount(self):        return self._cur.rowcount()    def commit(self):        self._conn.commit()    def rollback(self):        self._conn.rollback()    def __del__(self):        try:            self._cur.close()            self._conn.close()        except:            pass    def close(self):        self.__del__()

使用測試:

use.py

#!/usr/bin/env pythonfrom DB import ZDBdef main():    dbconfig={‘host‘:‘ ‘,              ‘port‘:3306,              ‘user‘:‘ ‘,              ‘passwd‘:‘ ‘,              ‘db‘:‘test‘,              ‘charset‘:‘UTF8‘}    db=ZDB(dbconfig)    sql = "SELECT * FROM `user`"    db.query(sql)    result = db.fetchAllRows()    print "This is the result>",result    for row in result:        for colum in row:            print colum    db.close()main()

 

使用命令 python use.py 進行調用  _____________________________________________________________ 也可以使用pymysql查詢簡單demo如下所示:
#!/usr/bin/env python# -*- coding: utf-8 -*-import pymysqlconn=pymysql.connect(host=‘localhost‘,                     port=3306,                     user=‘root‘,                     passwd=‘root‘,                     db=‘test‘,                     charset=‘utf8‘)cur = conn.cursor()sql = "SELECT * FROM chart_pie"cur.execute(sql)for r in cur.fetchall():    for column in r:        print(r)conn.close()

 

window10系統下使用python3.3版本實現mysql查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.