One, install Mysql-python
Python connects MySQL database requires Python interface to MySQL package, package name is
Mysql-python
, the PyPI is now in the 1.2.5 version. Mysql-python is installed under Windows through the installer of the. exe file.
If you have already installed Python that needs to be written to the registry, refer to this article:registration (Registry) issues after Windows installation python2.7.
And then start installing Mysql-python, but here's a pit, from the Mysql-python version of PyPI-
Mysql-python-1.2.5.win32-py2.7.exe (MD5)--not necessarily available, because the Python environment and Mysql-python may not match the number of software bits,
For example, my locally installed Python is 64 bits, and the Mysql-python downloaded from PyPI cannot be used.
will report such an error: python importerror:dll load failed:%1
Just install the correct number of Mysql-python on it, here to share the next 64 bits:
Mysql-python-1.2.5.win-amd64-py2.7.exe
Second, simple to use
The reference code is as follows:
1Python 2.7.6 (default, Nov, 19:24:24) [MSC v.1500 64bit (AMD64)] on Win322 ImportMySQLdb3conn = MySQLdb.connect (host='localhost', port=3306,user='Root', passwd='123456', db='Test')4Cur =conn.cursor ()5Cur.execute ('Select ' title ', ' text ' from ' entries ' limit')62L7 Cur.fetchall ()8(('Bokeyuan','Bokeyuan Text ...'), ('Google Translate','Google Translate text ...'))9 cur.close ()TenConn.close ()
Python connects to MySQL database under Windows