Python Operation SQLite Database

Source: Internet
Author: User
Tags sqlite database

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

Related Article

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