Python Operation SQLite

Source: Internet
Author: User

Tags: pytho cursor data any object database connection database object CHM Connect

1. Import Sqlite3 database module, from python2.5, sqlite3 become built-in module, do not need additional installation, only need to import.
import sqlite3
2. Create/Open Database
    • To open a database by using the Connect methodcon = sqlite3.connect(‘D:\test.db‘)
    • Not only can you create database files on your hard disk, but you can also create them in memory.
      con = sqlite3.connect(‘:memory:‘)
3. Database Connection objects
    • The Con object is returned by the Connect method above. That is, the database object, which provides the following methods:
      • The cursor () method is used to create a cursor object
      • Commit () method for transaction commit
      • The rollback () method is used to rollback a transaction
      • The close () method is used to close a database connection
4. Use of Cursor objects
    • To create a Cursor object:cur = con.cursor()

    • Cursor methods
      • Execute () to execute the SQL statement
      • Executemany () to execute multiple SQL statements
      • Close () Closes the cursor object
      • Fetchone () is used to fetch a record from the result and point to the next record
      • Fetchmany () take more than one record
      • Fetch () Fetch all records
      • Sroll () for cursor scrolling

Python Operation SQLite

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: