Python adds, deletes, modifies, and queries mysql, and pythonmysql

Source: Internet
Author: User

Python adds, deletes, modifies, and queries mysql, and pythonmysql

No database is available for BS or CS projects.

This article is a summary of python's operations on mysql. It is suitable for developers who have a certain degree of foundation. It is best to read it by people who study step by step. Because I think that life cannot always start from scratch, it is a disaster.

For installation or use of python2.7 and mysql5.7.11, check the official website documents whenever possible.

There are two main ways for python to command mysql to work: mysql. connector and MySQLdb.

I personally think that mysql. connector is more reliable. After all, it is official.

We do not recommend that you configure mysql_config and mysql environment variables for third-party MySQLdb installation. I won't use him either.

The download link for installing mysql. connector is as follows. The installation is simple.

Http://dev.mysql.com/downloads/connector/python/

There are two main types of database operations: Query and update. Updates are divided into addition, deletion, and modification.

Python operates mysql in five steps: connect, create a cursor, splice SQL statements, execute SQL statements, and obtain results.

1. Connection

Mysql_config = {'user': 'database username', 'Password': 'database password', 'host': 'database address', 'Port': Database port number, 'database': 'database name' 'charset': 'database Code'} conn = mysql. connector. connect (** mysql_config)

2. Create a cursor

cursor=conn.cursor()

3. concatenate SQL statements

SQL = "SQL statement to be executed"

4. Execute SQL

cursor=conn.cursor()cursor.execute(sql)

5. Get Results

result_set = cursor.fetchall()

If you do not understand the SQL statements, go to the SQL tutorial.

The format of the retrieved result set is incorrect. Go to the basic python tutorial.

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.