The first step is to install the MySQL repository.
Here I install the MARIADB library, version 5.5, and the character set is configured. This is not a detailed statement, I believe we have no problem.
The second step is to install MySQL drivers.
First, let's say that there are two main drivers:
Mysql-connector-python: MySQL is the official pure Python drive;
Mysql-python: is a python driver that encapsulates the MySQL C drive.
I've always liked the official stuff, and the quality is guaranteed.
Then install the Mysql-connector-python:
At the beginning, I intend to use command installation
Pip Install Mysql-connector-python--allow-external Mysql-connector-python
The result is not found in the version, then discovered that the PyPI on the mobile warehouse is empty, only one point to the official MySQL download connection, Nai had to download the installation.
Searching in PyPI's warehouse, the version I got was 2.0.4.
Download Mysql-connector-python-2.0.4.zip
Unzip Mysql-connector-python-2.0.4.zip
CD mysql-connector-python-2.0.4
Python setup.py Install
Step three: Use Python to connect to the repository
# import MySQL driver:
>>> Import Mysql.connector
# Be careful to set password as your root password:
>>> conn = Mysql.connector.connect (user= ' root ', password= ' password ', database= ' test ')
>>> cursor = conn.cursor ()
# Create User table:
>>> cursor.execute (' CREATE table user (ID varchar (primary key, name varchar (20)) ')
# Insert a row of records, note that the MySQL placeholder is%s:
>>> Cursor.execute (' INSERT into user (ID, name) values (%s,%s) ', [' 1 ', ' Michael '])
>>> Cursor.rowcount
1
# COMMIT TRANSACTION:
>>> Conn.commit ()
>>> Cursor.close ()
# Run Query:
>>> cursor = conn.cursor ()
>>> cursor.execute (' select * from user where id =%s ', (' 1 ',))
>>> values = Cursor.fetchall ()
>>> values
[(' 1 ', ' Michael ')]
# Close cursor and connection:
>>> Cursor.close ()
True
>>> Conn.close ()
Python connects MySQL repositories