#-*-Coding:utf-8-*-
# import SQLite driver:
>>> Import Sqlite3
# Connect to the SQLite database
# Database file is test.db
# If the file does not exist, it is automatically created in the current directory:
>>> conn = sqlite3.connect (' test.db ')
# Create a cursor:
>>> cursor = conn.cursor ()
# Execute an SQL statement to create the user table:
>>> cursor.execute (' CREATE table user (ID varchar (primary key, name varchar (20)) ')
<sqlite3. Cursor Object at 0x10f8aa260>
# Continue executing an SQL statement and insert a record:
>>> Cursor.execute (' INSERT into user (ID, name) VALUES (\ ' 1\ ', \ ' michael\ ') ')
<sqlite3. Cursor Object at 0x10f8aa260>
# Gets the number of rows inserted through ROWCOUNT:
>>> Cursor.rowcount
1
# Close cursor:
>>> Cursor.close ()
# COMMIT TRANSACTION:
>>> Conn.commit ()
# Close Connection:
>>> Conn.close ()
Query Record:
>>> conn = sqlite3.connect (' test.db ')
>>> cursor = conn.cursor ()
# Execute the query statement:
>>> cursor.execute (' select * from user where id=? ', (' 1 ',))
<sqlite3. Cursor Object at 0x10f8aa340>
# Get query result set:
>>> values = Cursor.fetchall ()
>>> values
[(' 1 ', ' Michael ')]
>>> Cursor.close ()
>>> Conn.close ()
Python's sqlite3 library learning