python3.6 connecting MySQL

Source: Internet
Author: User

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

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.