"""SQL 通用数据类型离线实验:在内存 SQLite 中验证标准声明类型亲和性、精确小数与近似浮点差异及跨库映射表。"""
import json
import platform
import sqlite3
def run() -> dict:
conn = sqlite3.connect(":memory:")
cases = []
# Case 1: Standard SQL declared column types -> SQLite storage classes via typeof()
conn.executescript(
"""
CREATE TABLE general_types_demo (
c_char CHARACTER(5),
c_varchar VARCHAR(50),
c_bin BINARY(4),
c_bool BOOLEAN,
c_small SMALLINT,
c_int INTEGER,
c_big BIGINT,
c_dec DECIMAL(5, 2),
c_num NUMERIC(5, 2),
c_real REAL,
c_float FLOAT,
c_double DOUBLE PRECISION,
c_date DATE,
c_time TIME,
c_ts TIMESTAMP
);
"""
)
conn.execute(
"""
INSERT INTO general_types_demo VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
);
""",
(
"Hi",
"SQL",
b"\x01\x02\x03\x04",
1,
32000,
42,
9000000000,
123.45,
67.89,
2.5,
3.14,
1.23456789,
"2026-05-01",
"12:30:00",
"2026-05-01 12:30:00",
),
)
row_types = conn.execute(
"""
SELECT
typeof(c_char), typeof(c_varchar), typeof(c_bin), typeof(c_bool),
typeof(c_small), typeof(c_int), typeof(c_big),
typeof(c_dec), typeof(c_num), typeof(c_real), typeof(c_float), typeof(c_double),
typeof(c_date), typeof(c_time), typeof(c_ts)
FROM general_types_demo;
"""
).fetchone()
col_names = [r[1] for r in conn.execute("PRAGMA table_info(general_types_demo);").fetchall()]
type_map = dict(zip(col_names, row_types))
exp1 = {
"c_char": "text",
"c_varchar": "text",
"c_bin": "blob",
"c_bool": "integer",
"c_small": "integer",
"c_int": "integer",
"c_big": "integer",
"c_dec": "real",
"c_num": "real",
"c_real": "real",
"c_float": "real",
"c_double": "real",
"c_date": "text",
"c_time": "text",
"c_ts": "text",
}
act1 = type_map
cases.append(
{
"name": "sqlite_general_type_affinities",
"summary": "验证 15 种常见标准 SQL 声明类型在 SQLite 类型亲和规则下的实际存储类(typeof)",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
}
)
# Case 2: Exact integer/decimal semantics vs binary floating point approximation
prec = conn.execute(
"""
SELECT
((0.1 + 0.2) = 0.3),
(ROUND(0.1 + 0.2, 2) = 0.3),
((10 + 20) = 30),
CAST(123.45 * 100 AS INTEGER);
"""
).fetchone()
exp2 = {
"ieee754_01_plus_02_eq_03": 0,
"rounded_eq_03": 1,
"exact_cents_eq_30": 1,
"decimal_5_2_in_cents": 12345,
}
act2 = {
"ieee754_01_plus_02_eq_03": prec[0],
"rounded_eq_03": prec[1],
"exact_cents_eq_30": prec[2],
"decimal_5_2_in_cents": prec[3],
}
cases.append(
{
"name": "decimal_vs_binary_float_precision",
"summary": "对比 IEEE 754 近似浮点(0.1+0.2 != 0.3)与定点数/分单位精确整数运算",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
}
)
# Case 3: Cross-DBMS general type quick-reference table audit in SQLite
conn.executescript(
"""
CREATE TABLE type_crosswalk (
category TEXT PRIMARY KEY,
access_type TEXT NOT NULL,
sqlserver_type TEXT NOT NULL,
oracle_type TEXT NOT NULL,
mysql_type TEXT NOT NULL,
postgres_type TEXT NOT NULL
);
INSERT INTO type_crosswalk VALUES
('boolean', 'Yes/No', 'Bit', 'Byte', 'N/A', 'Boolean'),
('integer', 'Number (integer)', 'Int', 'Number', 'Int / Integer', 'Int / Integer'),
('float', 'Number (single)', 'Float / Real', 'Number', 'Float', 'Numeric'),
('currency', 'Currency', 'Money', 'N/A', 'N/A', 'Money'),
('string (fixed)', 'N/A', 'Char', 'Char', 'Char', 'Char'),
('string (variable)', 'Text (<256) / Memo (65k+)', 'Varchar', 'Varchar / Varchar2', 'Varchar', 'Varchar'),
('binary object', 'OLE Object / Memo', 'Binary / Varbinary / Image', 'Long / Raw', 'Blob / Text', 'Binary / Varbinary');
"""
)
total_rows = conn.execute("SELECT COUNT(*) FROM type_crosswalk;").fetchone()[0]
na_count = conn.execute(
"""
SELECT SUM(
(access_type = 'N/A') +
(sqlserver_type = 'N/A') +
(oracle_type = 'N/A') +
(mysql_type = 'N/A') +
(postgres_type = 'N/A')
)
FROM type_crosswalk;
"""
).fetchone()[0]
exp3 = {"category_count": 7, "na_cell_count": 4}
act3 = {"category_count": total_rows, "na_cell_count": na_count}
cases.append(
{
"name": "cross_db_type_name_mapping",
"summary": "核对 7 类通用数据类型在五种主流数据库平台上的名称对照表及 4 处 N/A 差异项",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
}
)
conn.close()
return {
"version": f"CPython {platform.python_version()} / 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/sql-datatypes-general-lab/explore-datatypes-general.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-datatypes-general.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1532.zip
课程关联工程包(登记关联67章) · 含当前章节或实例 · mbskycom202612a1523.zip