一个用于解析和生成 URL 和查询字符串的 SQLite 扩展。基于 libcurl 的 URL API
在浏览器中尝试并在此处了解更多: Introducing sqlite-url: A SQLite extension for parsing and generating URLs (2022 年 9 月)
.load ./url0
select url_valid('https://sqlite.ac.cn'); -- 1
从给定的 URL 中提取特定部分。
select url_scheme('https://www.sqlite.org/vtab.html#usage'); -- 'https'
select url_host('https://www.sqlite.org/vtab.html#usage'); -- 'www.sqlite.org'
select url_path('https://www.sqlite.org/vtab.html#usage'); -- '/vtab.html'
select url_fragment('https://www.sqlite.org/vtab.html#usage'); -- 'usage'
以编程方式生成 URL。
select url(null,
'scheme', 'https',
'host', 'alexgarcia.url',
'fragment', 'yeet'
); -- 'https://alexgarcia.url/#yeet'
遍历 URL 查询字符串中的所有参数。
select *
from url_query_each(
url_query('https://api.census.gov/data/2020/acs/acs5?get=B01001_001E&for=county:*&in=state:06')
);
/*
┌──────┬─────────────┐
│ name │ value │
├──────┼─────────────┤
│ get │ B01001_001E │
│ for │ county:* │
│ in │ state:06 │
└──────┴─────────────┘
*/
与 sqlite-http 结合使用,用于生成请求的 URL。
select http_get_body(
url(
'https://api.census.gov',
'path', '/data/2020/acs/acs5',
'query', url_querystring(
'get', 'B01001_001E',
'for', 'county:*',
'in', 'state:06'
)
)
);
/*
┌────────────────────────────────────┐
│ http_get_body( │
├────────────────────────────────────┤
│ [["B01001_001E","state","county"], │
│ ["1661584","06","001"], │
│ ["1159","06","003"], │
│ ["223344","06","007"], │
│ ["21491","06","011"], │
│ ["1147788","06","013"], │
│ ["190345","06","017"], │
│ ... │
│ ["845599","06","111"]] │
└────────────────────────────────────┘
*/
与 sqlite-path 结合使用,用于安全地生成 URL 的路径。
select url(
'https://github.com',
'path', path_join('/', 'asg017', 'sqlite-url', 'issues', '1')
);
-- 'https://github.com/asg017/sqlite-url/issues/1'
请参阅 docs.md
获取完整的 API 参考。
语言 | 安装方式 | |
---|---|---|
Python | pip install sqlite-url |
|
Datasette | datasette install datasette-sqlite-url |
|
Node.js | npm install sqlite-url |
|
Deno | deno.land/x/sqlite_url |
|
Ruby | gem install sqlite-url |
|
GitHub 发布版本 |
发布页面 包含适用于 Linux amd64 和 MacOS amd64 的预构建二进制文件(不支持 arm)。
如果您想将 sqlite-url
用作 运行时可加载扩展,请从发布版本中下载 url0.dylib
(适用于 MacOS) 或 url0.so
(适用于 Linux) 文件,并将其加载到您的 SQLite 环境中。
注意:文件名中的
0
(url0.dylib
/url0.so
) 表示sqlite-url
的主版本号。当前sqlite-url
处于 v1 版本之前,因此未来的版本可能会有重大变更。
例如,如果您使用 SQLite CLI,可以这样加载库:
.load ./url0
select url_version();
-- v0.0.1
或者在 Python 中,使用内置的 sqlite3 模块:
import sqlite3
con = sqlite3.connect(":memory:")
con.enable_load_extension(True)
con.load_extension("./url0")
print(con.execute("select url_version()").fetchone())
# ('v0.0.1',)
或者在 Node.js 中,使用 better-sqlite3:
const Database = require("better-sqlite3");
const db = new Database(":memory:");
db.loadExtension("./url0");
console.log(db.prepare("select url_version()").get());
// { 'html_version()': 'v0.0.1' }
或者与 Datasette 结合使用:
datasette data.db --load-extension ./url0
- sqlite-path,用于解析/生成路径(与
url_path()
和url()
配合得很好) - sqlite-http,用于在 SQLite 中发出 HTTP 请求
- sqlite-html,用于解析 HTML 文档
- sqlite-lines,用于逐行读取大文件
- nalgeon/sqlean,一些用 C 语言编写的、预编译的方便的 SQLite 函数