MySQL database related operations in Python

Source: Internet
Author: User

Install a basic environment: 1 Install MySQL database,

This article uses the MARIADB database, similar to MySQL

1 mount the image file (this time using a local image file)

2 starting the MySQL database

3 Create a user name and password and refresh

Grant All (represents all operations) on . (Library. Table (all, can also be specified) to [email protected] (set username as root, link to local link) identified by ' roiot123 ';(set password)
flush privileges; (Refresh permissions)

4 Modifying the default character set:


Action in SERVER.CNF (must have space before, otherwise it won't work, can't write kanji)

Client.cnf Operation Ibid.

Restart Load Character Set

5 to see if the character set has finished loading


As above, it means the load is complete!!!

6 Creating a Database

2 Installing the Mysql-python module, responsible for the interconnection between MySQL and Python 1 installation

2 Check whether the installation is complete, if done, will not error

Two MySQL links basic operations and Instructions 1 Import mysqldb module

Import Name Rename Rename

2 Creating a connection

The required parameters are user and passwd other optional
Where user represents the database username, which is the user name and password initialized above, DB is the above initialized database, the host represents a local link, you can use IP address or domain name for remote link, CharSet represents the character set used by the link, if the UTF8 does not correspond to the above, Garbled behavior may occur

3 Initializing Cursors

4 use Cursors. The Execute (' SQL ') statement is responsible for passing messages to the MySQL database.

For database operation, add insert delete Delete change update etc Search Select Show etc.

5 Submit

A commit is required after creation, and if not submitted it does not take effect, and the submission uses the keyword of the link that was created.

6 Close Link

To close a link, you first need to close the cursor, followed by the link.

7 views

To view in the MySQL database:

Three database advanced 1 display settings

Show results similar to database query show and select
Display in the database

Create a data table again in case the query is used

1 To view the display


Use cursors. Fetchone () indicates that each time a row of operations is viewed, two represent two operations output together

2 Show all viewed results

3 Displays the specified quantity

4 for viewing using Cur.scroll (0, ' absolute ')

It restores the cursor to the starting position, can be viewed multiple times, and, if there is no configuration, defaults to the next row of the last query to start the query

2 Multiline Insert 1 is inserted using the FOR loop traversal:


View

2 inserting SQL statements in a separate way from Cur.execute


View

3 Inserting in multiple rows


View

When%s does not have double quotation marks:


View

Four: Database application: 1 Generate name

To view build results:

2 Determine if a table exists in the database

3 applying classes that encapsulate MySQL databases



To see if the build:

MySQL database related operations in Python

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.