SQLite database of Python Module

Source: Internet
Author: User
SQLite is a well-known open-source embedded database software. It can be embedded in other programs and provides SQL interfaces for query, which is very convenient. Its official site is http://www.sqlite.org.
Python in Windows already comes with the sqlite3 module, which can be used directly.
The Python database module has a unified interface standard, so database operations are in a unified mode, basically the following steps (assuming that the database module is named dB ):

1. Use dB. Connect to create a database connection. Assume that the connection object is Conn.
2. If the Data Warehouse does not return the result, you can directly use conn.exe cute to query the database. Depending on the database transaction isolation level, you may need to change the database to conn. Commit.
3. If you need to return the query result, use conn. cursor to create the cursor object cur, use cur.exe cute to query the database, and use cur. fetchall/cur. fetchone/cur. fetchmany to return the query result. Depending on the database transaction isolation level, you may need to change the database to conn. Commit.
4. Disable cur, Conn

The sqlite3 module is no exception. Let's look at the example below.

#-*-Encoding: gb2312 -*-
Import sqlite3

Conn = sqlite3.connect ("D:/AAA. DB ")
Conn. isolation_level = none # This is the transaction isolation level. By default, You need to commit yourself to modify the database. If it is set to none, it is automatically submitted for each modification; otherwise, it is ""
# Create a table
Conn.exe cute ("create table if not exists T1 (ID integer primary key autoincrement, name varchar (128), info varchar (128 ))")
# Insert data
Conn.exe cute ("insert into T1 (name, Info) values ('zhaowei', 'only a test ')")
# If the isolation level is not automatically submitted, You need to manually execute commit
Conn. Commit ()
# Obtain the cursor object
Cur = conn. cursor ()
# Query with a cursor to obtain the result
Cur.exe cute ("select * from T1 ")
# Getting all results
Res = cur. fetchall ()
Print 'row: ', cur. rowcount
# Cur. description is the description of the table structure.
Print 'desc', cur. Description
# The result returned by fetchall is a two-dimensional list.
For line in res:
For f in line:
Print F,
Print
Print '-' * 60

Cur.exe cute ("select * from T1 ")
# After this query, only one result is obtained, which is a one-dimensional list.
Res = cur. fetchone ()
Print 'row: ', cur. rowcount
For f in Res:
Print F,
Print
# Fetch another row
Res = cur. fetchone ()
Print 'row: ', cur. rowcount
For f in Res:
Print F,
Print
Print '-' * 60


Cur. Close ()
Conn. Close ()
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.

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.