"""SQLite 安装离线实验:验证本地 CPython 内置 SQLite 运行库版本、编译选项与零配置数据库读写能力。"""
import json
import platform
import sqlite3
import tempfile
from pathlib import Path
def run() -> dict:
cases = []
# Case 1: Inspect embedded SQLite version and SQLite version_info tuple
ver_str = sqlite3.sqlite_version
ver_info = sqlite3.sqlite_version_info
conn = sqlite3.connect(":memory:")
sql_ver = conn.execute("SELECT sqlite_version();").fetchone()[0]
exp1 = {
"major": 3,
"minor_ge_35": True,
"sql_matches_python_attr": True,
}
act1 = {
"major": ver_info[0],
"minor_ge_35": ver_info[1] >= 35,
"sql_matches_python_attr": (sql_ver == ver_str),
}
cases.append({
"name": "verify_embedded_sqlite_runtime",
"summary": "验证内置 SQLite 3 引擎版本查询函数 sqlite_version() 与运行时库一致",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Inspect compile options & default encoding
opts = {r[0] for r in conn.execute("PRAGMA compile_options;").fetchall()}
encoding = conn.execute("PRAGMA encoding;").fetchone()[0]
exp2 = {
"default_encoding": "UTF-8",
"has_fts5": True,
"has_threadsafe_option": True,
}
act2 = {
"default_encoding": encoding,
"has_fts5": "ENABLE_FTS5" in opts,
"has_threadsafe_option": any(o.startswith("THREADSAFE=") for o in opts),
}
cases.append({
"name": "inspect_compile_options_and_encoding",
"summary": "核对本地 SQLite 引擎默认 UTF-8 编码、线程安全编译标志与 FTS5 扩展支持",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Zero-config file database round-trip smoke test (no daemon/server needed)
with tempfile.TemporaryDirectory() as tmpdir:
db_file = Path(tmpdir) / "install_check.db"
c1 = sqlite3.connect(str(db_file))
c1.execute("CREATE TABLE check_tbl (k TEXT PRIMARY KEY, v INTEGER);")
c1.execute("INSERT INTO check_tbl VALUES ('status', 200);")
c1.commit()
c1.close()
c2 = sqlite3.connect(str(db_file))
row = c2.execute("SELECT k, v FROM check_tbl;").fetchone()
c2.close()
exp3 = ["status", 200]
act3 = list(row)
cases.append({
"name": "zero_config_file_roundtrip",
"summary": "验证无需安装独立服务进程即可创建本地 .db 文件并跨连接持久读取数据",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
conn.close()
return {
"version": f"CPython {platform.python_version()} / Native SQLite {sqlite3.sqlite_version}",
"cases": cases,
}
if __name__ == "__main__":
print(json.dumps(run(), ensure_ascii=False, indent=2))
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/sqlite-installation-lab/explore-installation.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-installation.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1703.zip
课程关联工程包(登记关联51章) · 含当前章节或实例 · mbskycom202612a1680.zip