Download the corresponding version according to the number of Python bits:
32-bit: https://pypi.python.org/pypi/MySQL-python/1.2.5
64-bit: Http://arquivos.victorjabur.com/python/modules/MySQL-python-1.2.3.win-amd64-py2.7.exe
Download the corresponding version and install it directly.
Enter the code test:
- Import MySQLdb
- #-----------MySQL test code-------------
- Def mysql_test ():
- Try:
- Sql=' select * from student '
- Conn=mysqldb.connect (host=' 127.0.0.1 ', user=' root ', passwd=' test ', db=' test ', port=3306)
- Cur=conn.cursor ()
- Cur.execute (SQL)
- print ' conn:%s '%conn
- print ' cur:%s '%cur
- Cur.close ()
- Conn.close ()
- except Mysqldb.error,e:
- print ' MySQL Error%d:%s '% (e.args[0],e.args[1])
- If __name__=="__main__":
- Mysql_test ()
The test results show that the connection was successful:
- Conn:<_mysql.connection open to ' 127.0.0.1 ' at 2d01b28>
- Cur: <mysqldb.cursors.cursor object at 0x00000000031f9ef0>
If you see the following error, you should change localhost to 127.0.0.1.
- MySQL error 2013:lost connection to MySQL server @ ' waiting for initial communication packet ', system error: 0
Installing the MySQLdb module for Windows python2.7