"""SQL SELECT 离线实验:验证指定列投影、通配符全列读取与游标结果集逐行导航。"""
import json
import platform
import sqlite3
def seed_websites(conn: sqlite3.Connection) -> None:
conn.executescript(
"""
CREATE TABLE websites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
url TEXT NOT NULL,
alexa INTEGER NOT NULL,
country TEXT NOT NULL
);
INSERT INTO websites (id, name, url, alexa, country) VALUES
(1, 'Google', 'https://www.google.com/', 1, 'USA'),
(2, '淘宝', 'https://www.taobao.com/', 13, 'CN'),
(3, '蓝色天空', 'http://www.mbsky.com/', 4689, 'CN'),
(4, '微博', 'http://weibo.com/', 20, 'CN'),
(5, 'Facebook', 'https://www.facebook.com/', 3, 'USA');
"""
)
def run() -> dict:
conn = sqlite3.connect(":memory:")
seed_websites(conn)
cases = []
# Case 1: SELECT name, country FROM websites
cur1 = conn.execute("SELECT name, country FROM websites ORDER BY id;")
cols1 = [d[0] for d in cur1.description]
rows1 = [list(r) for r in cur1.fetchall()]
exp1 = {
"columns": ["name", "country"],
"rows": [
["Google", "USA"],
["淘宝", "CN"],
["蓝色天空", "CN"],
["微博", "CN"],
["Facebook", "USA"],
],
}
act1 = {"columns": cols1, "rows": rows1}
cases.append(
{
"name": "select_specific_columns",
"summary": "执行 SELECT name, country FROM websites 验证指定列投影与结果集列名",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
}
)
# Case 2: SELECT * FROM websites
cur2 = conn.execute("SELECT * FROM websites ORDER BY id;")
cols2 = [d[0] for d in cur2.description]
rows2 = [list(r) for r in cur2.fetchall()]
exp2 = {
"columns": ["id", "name", "url", "alexa", "country"],
"row_count": 5,
"first_row": [1, "Google", "https://www.google.com/", 1, "USA"],
"last_row": [5, "Facebook", "https://www.facebook.com/", 3, "USA"],
}
act2 = {
"columns": cols2,
"row_count": len(rows2),
"first_row": rows2[0],
"last_row": rows2[-1],
}
cases.append(
{
"name": "select_all_columns_star",
"summary": "执行 SELECT * FROM websites 验证星号通配符返回全部 5 列与 5 条记录",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
}
)
# Case 3: Programmatic cursor navigation over result set
cur3 = conn.execute("SELECT id, name FROM websites ORDER BY id;")
first = list(cur3.fetchone())
next_two = [list(r) for r in cur3.fetchmany(2)]
rest = [list(r) for r in cur3.fetchall()]
exhausted = cur3.fetchone()
exp3 = {
"first": [1, "Google"],
"next_two": [[2, "淘宝"], [3, "蓝色天空"]],
"rest": [[4, "微博"], [5, "Facebook"]],
"exhausted_is_none": True,
}
act3 = {
"first": first,
"next_two": next_two,
"rest": rest,
"exhausted_is_none": exhausted is None,
}
cases.append(
{
"name": "result_set_cursor_navigation",
"summary": "通过 fetchone/fetchmany/fetchall 验证结果集游标顺序导航与末尾耗尽",
"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-select-lab/explore-select.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-select.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联67章) · 含当前章节或实例 · mbskycom202612a1523.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1576.zip