MySQL Query using python3.3 version under WINDOW10 system

Source: Internet
Author: User
Tags mysql query

Reference Documentation:

The Mysql-python (MYSQLDB) package class of the Rabbit Warrior

Python installation module error (Importerror:no module named setuptools) workaround

Environmental Windows10 | python3.3

1. Ensure that Setuptools is installedThe following method:> download the installed script https://bootstrap.pypa.io/ez_setup.py, download the script and run >python ez_setup.py >. The script automatically determines the Python version, automatically downloads, installs. 2. Install PIPThe method is as follows: We also need to download on the official website of Python: https://pypi.python.org/pypi/pip#downloads: Unzip to a directory, CD in, use command python setup.py install For installation, add the X:\Python\Script directory to path 3. Installing the Mysqldbpip install Mysql-python test file: db.py
#!/usr/bin/env pythonImportMySQLdbImport TimeclassZdb:error_code="'_instance=None _conn=None _cur=None _timeout= 30_timecount=0def __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'])        exceptMysqldb.error,e:self.error_code=E.args[0] Error_msg="MYSQL ERROR!", e.args[0].e.args[1]            Printerror_msgifSelf._timecount <Self._timeout:interval= 5Self._timecount+=interval Time.sleep (interval)returnSelf.__init__(dbconfig)Else:                RaiseException (error_msg) self._cur=self._conn.cursor () self._instance=MySQLdbdefquery (self,sql):Try: Self._cur.execute ("SET NAMES UTF8") Result=self._cur.execute (SQL)exceptMysqldb.error,e:self.error_code=E.args[0]Print "MYSQL error-query:", e.args[0],e.args[1] Result=FALSEreturnresultdefUpdate (SELF,SQL):Try: Self._cur.execute ("SET NAMES UTF8") Result=self._cur.execute (SQL) Self._conn.commit ()exceptMysqldb.error,e:self.error_code=E.args[0]Print "MYSQL error-update:", e.args[0],e.args[1] Result=FALSEreturnresultdefInsert (self,sql):Try: Self._cur.execute ("SET NAMES UTF8") self._cur.execute (SQL) Self._conn.commit ()returnself._conn.insert_id ()exceptMysqldb.error,e:self.error_code=E.args[0]Print "MYSQL Error-insert:", e.args[0],e.args[1] Result=FALSEdeffetchallrows (self):returnSelf._cur.fetchall ()defGetRowCount (self):returnSelf._cur.rowcount ()defcommit (self): Self._conn.commit ()defrollback (self): Self._conn.rollback ()def __del__(self):Try: Self._cur.close () self._conn.close ()except:            Pass    defClose (self): self.__del__()

To use the test:

use.py

#!/usr/bin/env python fromDbImportZDBdefMain (): Dbconfig={'Host':' ',              'Port': 3306,              'User':' ',              'passwd':' ',              'DB':'Test',              'CharSet':'UTF8'} DB=ZDB (dbconfig) SQL="SELECT * from ' user '"db.query (SQL) result=db.fetchallrows ()Print "The result>", result forRowinchResult: forColuminchrow:PrintColum Db.close () main ()

Using the command Python use.py to invoke _____________________________________________________________ can also use the Pymysql query simple demo as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-ImportPymysqlconn=pymysql.connect (host='localhost', Port=3306, the user='Root', passwd='Root', DB='Test', CharSet='UTF8') cur=conn.cursor () SQL="SELECT * from Chart_pie"cur.execute (SQL) forRinchCur.fetchall (): forColumninchr:Print(R) conn.close ()

MySQL Query using python3.3 version under WINDOW10 system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.