python operations before working with the SQLite database operations database:1. Importing database ModulesImportSqlite3//after Python2.5, the SQLite3 module is built-in and can be imported directly when used.2. Create/Open Database CX= Sqlite3.connect ("e:/test.db") //connect the database using the Connect function, when calling the Connect function, you need to specify the path and name of the database, if the specified database exists directly open the database, if it does not exist, create a new one and then open it. Con= Sqlite3.connect (": Memory:") //You can also create a database in memory. 3. Database Connection object The object that is returned when the database is opened CX is a database connection object that can have the following actions: Commit ()--Transaction Commit rollback ()--transaction rollback close ()--Close a database connection cursor ()--Create a cursor4querying database cu using Cursors=cx.cursor ()//we need to use the cursor object to query the database using the SQL statement and get the query object. The cursor object has the following actions: Execute ()--Execute SQL statement Executemany--execute multiple SQL statements close ()--close Cursor Fetchone ()--take a record from the result and point the cursor to the next record Fetchmany ()--take multiple records from the results fetchall ()--remove all records from the results scroll ()--cursor Scrolling uses Python to manipulate the database:1. Build Table Cu.execute ("CREATE TABLE catalog (ID integer primary key,pid integer,name varchar () unique,nickname text NULL)") //The above statement creates a table called catalog, which has a primary key ID, a PID, and a name,name that cannot be duplicated, and a nickname default is null. 2. Inserting Data forTinch[(0,10,'ABC','Yu'), (1, 20,'CBA','Xu')]: Cx.execute ("INSERT INTO catalog values (?,?,?,?)", T) cx.commit ()3. Query Cu.execute ("SELECT * FROM Catalog") Cu.fetchall ()4. Modify Cu.execute ("Update Catalog Set name= ' boy ' where id = 0") Cx.commit ()5. Delete Cu.execute ("Delete from catalog where id = 1") Cx.commit ()
Python Operation SQLite Database