Python connects to MySQL database

Source: Internet
Author: User

1. Download http://dev.mysql.com/downloads/connector/python/

because Python installs 3.4, you need to download the following mysql-connector-python:after successful download:2, install Mysql-connector-python, you will find the installation of silent, you feel what all the wood has occurred, in fact, a lot of things happened, I observed that the Python installation directory under the more write things:MySQL folder more things, but also more mysql_connecter_....-info3. So you can connect to MySQL database using python ~ ~The test code is as follows:
ImportMysql.connectorconfig={'Host':'127.0.0.1',#default 127.0.0.1        'User':'Root',        'Password':'123456',        'Port': 3306,#The default is 3306        'Database':'School_data',        'CharSet':'UTF8'#The default is UTF8        }Try: CNN=mysql.connector.connect (* *config); Print("Connection Successful"); Cour=cnn.cursor (); Value=('1211','asddd'); SQL="""INSERT INTO DW (DWDM,DWMC) VALUES (%s,%s)""";    Cour.execute (Sql,value); #The following sentence is very important I am less this sentence lead to not error, also plug in data, collapse AH ~ ~ ~cnn.commit ();exceptMysql.connector.Error as E:Print('Connect fails! {}'. Format (e))finally: Cour.close (); Cnn.close ();

Python connects to MySQL database

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.