Sqlite3 - 不需要单独的服务器进程

sqlite3 模块由 GerhardHäring 编写。要使用该模块,必须首先创建一个表示数据库的 Connection 对象。这里的数据将存储在 example.db 文件中:

import sqlite3
conn = sqlite3.connect('example.db')

你还可以提供特殊名称:memory:在 RAM 中创建数据库。一旦有了 Connection,就可以创建一个 Cursor 对象并调用它的 execute() 方法来执行 SQL 命令:

c = conn.cursor()

# Create table
c.execute('''CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real)''')

# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

# Save (commit) the changes
conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()