sqlite
提供 sqlite_open、one、all、exec、transaction 和 query().bind() 等 SQLite 能力。
bt install sqlite
bt install sqlite 1.0.0
公开 API
sqlite_open()返回 SqliteSqliteone、all、exec、transaction、query、closeSqliteQuerybind、one、all、exec、close
sqlite
BT 官方 SQLite shared WASM 扩展,用于在 BT 项目中直接访问本地 SQLite 数据库文件。
安装
bt install sqlite
指定版本:
bt install sqlite 1.0.0
安装后扩展包会进入当前项目:
extensions/sqlite/sqlite-1.0.0.bts
能力
-
sqlite_open(path, options)打开 SQLite 数据库并返回Sqlite对象。 -
one(sql, params)查询单行;无结果返回empty,SQLiteNULL返回null。 -
all(sql, params)查询多行,并受max_rows与max_result_bytes限制。 -
exec(sql, params)执行写入语句并返回影响行数。 -
transaction(statements)串行执行同一事务中的多条语句。 -
query(sql).bind(value)提供链式参数绑定。
权限
该扩展需要读写项目目录内的数据库文件:
{ "fs_read": true, "fs_write": true, "net": false, "http": false, "process": false, "env": false }
示例
fs('@/data').create_dir() db = sqlite_open('@/data/app.db', { wal: true, busy_timeout_ms: 1000, max_rows: 100, max_result_bytes: 1048576 }) db.exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)', []) db.exec('INSERT INTO users (name) VALUES (?)', ['Alice']) row = db.one('SELECT name FROM users WHERE id = ?', [1]) // 输出:Alice print row.name db.close()