Python Chapter 4-Database Support

Source: Internet
Author: User

1. database Connection: Use the connect function. This function has multiple parameters. For example, dsn indicates the data source name. This parameter indicates the database dependency, user indicates the connection username, and password indicates the connection password, host indicates the host Name of the database, and database indicates the name of the database to be connected. The connect function returns the connection object, which indicates the current session with the database. It has four methods: close (), commit (), rollback (), and cursor. Shows the methods and features of the cursor:



Cursor is similar to preparestatement in java.
2. An example of creating a table and filling in table data:

In this example, convert converts the data of each row read from the abbrev.txt file so that the data can match the field type of the database. First, we need to import sqlite3 to introduce the sqlite3 module before using it for database connection and related operations. If no foo. db database file exists before the operation, the program will create a new foo. db database file.
Next is a database query Program (food_query.py ):

In this program, the command line parameters are obtained through the sys module as the where clause of the query, so we can call this program: python food_query.py "1 = 1 and a <10 order by B ".
Author: uohzoaix

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.