Python connects MySQL repositories

Source: Internet
Author: User

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

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.