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