MySQL database operations in Python practice

Source: Internet
Author: User
To enable Python to operate MySQL databases, you must first install the MySQL-python package. On CentOS, run the following command to install

To enable Python to operate MySQL databases, you must first install the MySQL-python package. On CentOS, run the following command to install

1. To enable Python to operate the MySQL database, first install the MySQL-python package. You can run the following command on CentOS to install the package:

[Python]

  • $ Sudo yum install MySQL-python
  • 2. Leave nothing to say. Take two steps. The following program creates a connection to the mysql database, executes a simple query, and prints the query result.

    [Python]

    3. The following is a database table creation and insertion Operation.

    [Python]

    4. next let's look at the query. there are usually two ways to query: one is to use cursor. fetchall () is used to obtain all query results and then iterate over one row. fetchone () gets a record until the obtained result is null. Let's take a look at the following example:

    [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.