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