"""SQL WHERE 离线实验:验证文本/数值条件过滤及 9 类常用比较、范围、模式与集合运算符。"""
import json
import platform
import sqlite3
def run() -> dict:
conn = sqlite3.connect(":memory:")
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');
"""
)
cases = []
# Case 1: Text literal with single quotes
cn_names = [
r[0]
for r in conn.execute(
"SELECT name FROM websites WHERE country = 'CN' ORDER BY id;"
).fetchall()
]
exp1 = ["淘宝", "蓝色天空", "微博"]
act1 = cn_names
cases.append(
{
"name": "where_text_literal_quotes",
"summary": "执行 WHERE country='CN' 验证文本字面量单引号匹配返回 3 条中国网站记录",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
}
)
# Case 2: Numeric literal without quotes
id1 = list(conn.execute("SELECT id, name, country FROM websites WHERE id = 1;").fetchone())
exp2 = [1, "Google", "USA"]
act2 = id1
cases.append(
{
"name": "where_numeric_literal_no_quotes",
"summary": "执行 WHERE id=1 验证数值字段无需引号直接按整型精确筛选",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
}
)
# Case 3: Comparison operators =, <>, !=, >, <, >=, <=
eq_res = [r[0] for r in conn.execute("SELECT name FROM websites WHERE alexa = 13;").fetchall()]
ne_sql = [r[0] for r in conn.execute("SELECT name FROM websites WHERE country <> 'CN' ORDER BY id;").fetchall()]
ne_bang = [r[0] for r in conn.execute("SELECT name FROM websites WHERE country != 'CN' ORDER BY id;").fetchall()]
gt_res = [r[0] for r in conn.execute("SELECT name FROM websites WHERE alexa > 20 ORDER BY id;").fetchall()]
lt_res = [r[0] for r in conn.execute("SELECT name FROM websites WHERE alexa < 13 ORDER BY id;").fetchall()]
ge_res = [r[0] for r in conn.execute("SELECT name FROM websites WHERE alexa >= 20 ORDER BY id;").fetchall()]
le_res = [r[0] for r in conn.execute("SELECT name FROM websites WHERE alexa <= 3 ORDER BY id;").fetchall()]
exp3 = {
"eq": ["淘宝"],
"ne_standard": ["Google", "Facebook"],
"ne_bang_equal": True,
"gt": ["蓝色天空"],
"lt": ["Google", "Facebook"],
"ge": ["蓝色天空", "微博"],
"le": ["Google", "Facebook"],
}
act3 = {
"eq": eq_res,
"ne_standard": ne_sql,
"ne_bang_equal": ne_sql == ne_bang,
"gt": gt_res,
"lt": lt_res,
"ge": ge_res,
"le": le_res,
}
cases.append(
{
"name": "where_comparison_operators",
"summary": "逐一实测 =、<>、!=、>、<、>=、<= 七种比较运算符在 WHERE 中的筛选结果",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
}
)
# Case 4: BETWEEN, LIKE, IN operators in WHERE
between_res = [
r[0]
for r in conn.execute(
"SELECT name FROM websites WHERE alexa BETWEEN 3 AND 20 ORDER BY alexa;"
).fetchall()
]
like_res = [
r[0]
for r in conn.execute(
"SELECT name FROM websites WHERE name LIKE '%oo%' ORDER BY id;"
).fetchall()
]
in_res = [
r[0]
for r in conn.execute(
"SELECT name FROM websites WHERE id IN (2, 4) ORDER BY id;"
).fetchall()
]
exp4 = {
"between_3_20": ["Facebook", "淘宝", "微博"],
"like_oo": ["Google", "Facebook"],
"in_2_4": ["淘宝", "微博"],
}
act4 = {
"between_3_20": between_res,
"like_oo": like_res,
"in_2_4": in_res,
}
cases.append(
{
"name": "where_between_like_in_operators",
"summary": "实测 WHERE 子句中的 BETWEEN 范围、LIKE 模式与 IN 多值集合运算符",
"expected": exp4,
"actual": act4,
"passed": act4 == exp4,
}
)
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-where-lab/explore-where.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-where.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联67章) · 含当前章节或实例 · mbskycom202612a1523.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1586.zip