Learn python for the first time because the MySQL driver module needs to be installed when Python connects to MySQL
Before installing the MySQL driver method according to the method on the Liaoche website:
MySQL officially provides the Mysql-connector-python driver, but when installed, you need to add parameters to the PIP command --allow-external :
$ pip install mysql-connector-python --allow-external mysql-connector-python
If the above command fails to install, you can try another driver:
$ pip Install Mysql-connector
Both of these methods were tried, and none of them succeeded.
later found that, because the use of python3.6, as if the above two methods are provided to the previous version of Python, but python3.6 is not loaded, has been reported a configuration file has a problem
And then found a solution in http://blog.csdn.net/wochunyang/article/details/52457969.
install Pymysql with PIP:
Pip Install Pymysql
Example of connection database code:
#coding =utf-8
#导入pymysql的包
Import PymysqlImport Pymysql.cursors
#获取一个数据库连接, note that if you have a UTF-8 type, you need to make a database
#port must be a number cannot be a string
Connection=pymysql.connect (host=' localhost ', user=' Root ', password=' 123456 ', db=' Test ', port=3307, charset=' UTF8 ')
Try
#获取一个游标
With Connection.cursor () as cursor:
Sql=' select * from user ' Cout=cursor.execute (SQL)
Print ("Qty:" +str (cout))
For row in Cursor.fetchall ():
#print ('%s\t%s\t%s '%row)
#注意int类型需要使用str函数转义
Print ("ID:" +str (row[0]) +' name: ' +row[1]+"Gender:" +row[2])
Connection.commit ()
Finally
Connection.close ()
python3.6 connecting MySQL