"""SQLite 数据类型离线实验:实测五大存储类、五类列亲和性转换规则、类型名称映射、布尔 0/1 与三类日期时间存储。"""
import json
import platform
import sqlite3
def run() -> dict:
conn = sqlite3.connect(":memory:")
cases = []
# Case 1: Five storage classes (NULL, INTEGER, REAL, TEXT, BLOB) + dynamic typing
conn.execute("CREATE TABLE dyn (id INTEGER PRIMARY KEY, val);")
conn.executemany(
"INSERT INTO dyn (id, val) VALUES (?, ?);",
[
(1, None),
(2, 42),
(3, 3.14),
(4, "SQLite"),
(5, b"\xde\xad\xbe\xef"),
],
)
types_row = [r[0] for r in conn.execute("SELECT typeof(val) FROM dyn ORDER BY id;").fetchall()]
exp1 = ["null", "integer", "real", "text", "blob"]
cases.append({
"name": "five_storage_classes_dynamic_typing",
"summary": "验证动态类型系统下同一列可存放 NULL、INTEGER、REAL、TEXT、BLOB 五种存储类",
"expected": exp1,
"actual": types_row,
"passed": types_row == exp1,
})
# Case 2: Five column affinities (TEXT, NUMERIC, INTEGER, REAL, NONE/BLOB)
conn.execute("""
CREATE TABLE aff_demo (
c_text TEXT,
c_num NUMERIC,
c_int INTEGER,
c_real REAL,
c_none BLOB
);
""")
conn.execute("INSERT INTO aff_demo VALUES (12345, '30000.0', '30000.0', '30000.0', '30000.0');")
aff_types = list(conn.execute(
"SELECT typeof(c_text), typeof(c_num), typeof(c_int), typeof(c_real), typeof(c_none) FROM aff_demo;"
).fetchone())
# Also test non-numeric text into NUMERIC stays TEXT, and CAST difference between INTEGER and NUMERIC
conn.execute("DELETE FROM aff_demo;")
conn.execute("INSERT INTO aff_demo VALUES (10, 'abc', '10.5', 10, 10);")
aff_edge = list(conn.execute(
"SELECT typeof(c_text), typeof(c_num), typeof(c_int), typeof(c_real), typeof(c_none) FROM aff_demo;"
).fetchone())
cast_diff = list(conn.execute("SELECT typeof(CAST('3.9' AS INTEGER)), CAST('3.9' AS INTEGER), typeof(CAST('3.9' AS NUMERIC)), CAST('3.9' AS NUMERIC);").fetchone())
exp2 = {
"from_30000_dot_0": ["text", "integer", "integer", "real", "text"],
"edge_conversions": ["text", "text", "real", "real", "integer"],
"cast_int_vs_numeric": ["integer", 3, "real", 3.9],
}
act2 = {
"from_30000_dot_0": aff_types,
"edge_conversions": aff_edge,
"cast_int_vs_numeric": cast_diff,
}
cases.append({
"name": "column_affinity_conversion_rules",
"summary": "验证 TEXT/NUMERIC/INTEGER/REAL/NONE 五种亲和性对 '30000.0'、非数值文本及 CAST 的转换差异",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Declared SQL type names -> SQLite affinity mapping
conn.execute("""
CREATE TABLE type_names (
a_int SMALLINT,
a_bigint UNSIGNED BIG INT,
b_varchar VARCHAR(255),
b_clob CLOB,
c_blob BLOB,
c_untyped,
d_double DOUBLE PRECISION,
d_float FLOAT,
e_decimal DECIMAL(10,5),
e_datetime DATETIME
);
""")
conn.execute("INSERT INTO type_names VALUES ('7.0','8.0', 99, 100, '42', '42', '30.0', 5, '30.0', '2026');")
mapped = list(conn.execute(
"SELECT typeof(a_int), typeof(a_bigint), typeof(b_varchar), typeof(b_clob), "
"typeof(c_blob), typeof(c_untyped), typeof(d_double), typeof(d_float), "
"typeof(e_decimal), typeof(e_datetime) FROM type_names;"
).fetchone())
exp3 = ["integer", "integer", "text", "text", "text", "text", "real", "real", "integer", "integer"]
cases.append({
"name": "declared_type_names_to_affinity",
"summary": "验证 SMALLINT/VARCHAR/CLOB/BLOB/未指定/DOUBLE PRECISION/DECIMAL/DATETIME 的实际亲和性",
"expected": exp3,
"actual": mapped,
"passed": mapped == exp3,
})
# Case 4: Boolean (0/1) and Date/Time in TEXT, REAL (Julian day), INTEGER (Unix epoch)
conn.execute("CREATE TABLE flags_dates (flag BOOLEAN, dt_text TEXT, dt_julian REAL, dt_unix INTEGER);")
conn.execute("""
INSERT INTO flags_dates VALUES (
TRUE,
'2026-01-01 00:00:00',
julianday('2026-01-01 00:00:00'),
unixepoch('2026-01-01 00:00:00')
);
""")
row = conn.execute(
"SELECT flag, typeof(flag), dt_text, typeof(dt_text), "
"ROUND(dt_julian, 1), typeof(dt_julian), dt_unix, typeof(dt_unix), "
"datetime(dt_julian), datetime(dt_unix, 'unixepoch') FROM flags_dates;"
).fetchone()
exp4 = {
"flag_val": 1,
"flag_type": "integer",
"dt_text": "2026-01-01 00:00:00",
"dt_text_type": "text",
"dt_julian": 2461041.5,
"dt_julian_type": "real",
"dt_unix": 1767225600,
"dt_unix_type": "integer",
"julian_back_to_iso": "2026-01-01 00:00:00",
"unix_back_to_iso": "2026-01-01 00:00:00",
}
act4 = {
"flag_val": row[0],
"flag_type": row[1],
"dt_text": row[2],
"dt_text_type": row[3],
"dt_julian": row[4],
"dt_julian_type": row[5],
"dt_unix": row[6],
"dt_unix_type": row[7],
"julian_back_to_iso": row[8],
"unix_back_to_iso": row[9],
}
cases.append({
"name": "boolean_and_datetime_storage_classes",
"summary": "验证布尔值存储为整数 0/1,以及日期时间在 TEXT、REAL(儒略日) 与 INTEGER(Unix 秒) 间的无损互转",
"expected": exp4,
"actual": act4,
"passed": act4 == exp4,
})
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-data-types-lab/explore-data-types.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-data-types.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联51章) · 含当前章节或实例 · mbskycom202612a1680.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1687.zip