sqlite3 --- SQLite 資料庫的 DB-API 2.0 介面¶
原始碼:Lib/sqlite3/
SQLite is a C library that provides a lightweight disk-based database that doesn't require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It's also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.
The sqlite3 module was written by Gerhard Häring. It provides an SQL interface
compliant with the DB-API 2.0 specification described by PEP 249, and
requires the third-party SQLite library.
This is an optional module. If it is missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see 可選模組的需求.
此文件包含四個主要章節:
教學 教導如何使用
sqlite3模組。Reference 描述此模組定義的類別與函式。
How-to guides 詳細說明如何處理特定工作。
解釋 深入提供交易 (transaction) 控制的背景。
也參考
- https://www.sqlite.org
The SQLite web page; the documentation describes the syntax and the available data types for the supported SQL dialect.
- https://www.w3schools.com/sql/
Tutorial, reference and examples for learning SQL syntax.
- PEP 249 - 資料庫 API 規格 2.0
PEP 由 Marc-André Lemburg 撰寫。
教學¶
In this tutorial, you will create a database of Monty Python movies
using basic sqlite3 functionality.
It assumes a fundamental understanding of database concepts,
including cursors and transactions.
First, we need to create a new database and open
a database connection to allow sqlite3 to work with it.
Call sqlite3.connect() to create a connection to
the database tutorial.db in the current working directory,
implicitly creating it if it does not exist:
import sqlite3
con = sqlite3.connect("tutorial.db")
The returned Connection object con
represents the connection to the on-disk database.
In order to execute SQL statements and fetch results from SQL queries,
we will need to use a database cursor.
Call con.cursor() to create the Cursor:
cur = con.cursor()
Now that we've got a database connection and a cursor,
we can create a database table movie with columns for title,
release year, and review score.
For simplicity, we can just use column names in the table declaration --
thanks to the flexible typing feature of SQLite,
specifying the data types is optional.
Execute the CREATE TABLE statement
by calling cur.execute(...):
cur.execute("CREATE TABLE movie(title, year, score)")
We can verify that the new table has been created by querying
the sqlite_master table built-in to SQLite,
which should now contain an entry for the movie table definition
(see The Schema Table for details).
Execute that query by calling cur.execute(...),
assign the result to res,
and call res.fetchone() to fetch the resulting row:
>>> res = cur.execute("SELECT name FROM sqlite_master")
>>> res.fetchone()
('movie',)
We can see that the table has been created,
as the query returns a tuple containing the table's name.
If we query sqlite_master for a non-existent table spam,
res.fetchone() will return None:
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'")
>>> res.fetchone() is None
True
Now, add two rows of data supplied as SQL literals
by executing an INSERT statement,
once again by calling cur.execute(...):
cur.execute("""
INSERT INTO movie VALUES
('Monty Python and the Holy Grail', 1975, 8.2),
('And Now for Something Completely Different', 1971, 7.5)
""")
The INSERT statement implicitly opens a transaction,
which needs to be committed before changes are saved in the database
(see Transaction control for details).
Call con.commit() on the connection object
to commit the transaction:
con.commit()
We can verify that the data was inserted correctly
by executing a SELECT query.
Use the now-familiar