@tauri-apps/plugin-sql
Database
Database 类是与 SQL 插件的 Rust 端进行通信的主要接口。
new default(path): default| 参数 | 类型 |
|---|---|
path | string |
源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L29
| 属性 | 类型 | 定义于 |
|---|---|---|
path | string | 源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L28 |
close(db?): Promise<boolean>close
关闭数据库连接池。
| 参数 | 类型 | 描述 |
|---|---|---|
db? | string | 如果您正在管理多个数据库,可以选填数据库名称。否则,所有数据库连接池都将在作用域内。 |
Promise<boolean>
const success = await db.close()源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L162
execute(query, bindValues?): Promise<QueryResult>execute
将 SQL 表达式传递给数据库执行。
| 参数 | 类型 |
|---|---|
query | string |
bindValues? | 未知[] |
// for sqlite & postgres// INSERT exampleconst result = await db.execute( "INSERT into todos (id, title, status) VALUES ($1, $2, $3)", [ todos.id, todos.title, todos.status ]);// UPDATE exampleconst result = await db.execute( "UPDATE todos SET title = $1, completed = $2 WHERE id = $3", [ todos.title, todos.status, todos.id ]);
// for mysql// INSERT exampleconst result = await db.execute( "INSERT into todos (id, title, status) VALUES (?, ?, ?)", [ todos.id, todos.title, todos.status ]);// UPDATE exampleconst result = await db.execute( "UPDATE todos SET title = ?, completed = ? WHERE id = ?", [ todos.title, todos.status, todos.id ]);源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L108
select<T>(query, bindValues?): Promise<T>select
将 SELECT 查询传递给数据库执行。
| 类型参数 |
|---|
T |
| 参数 | 类型 |
|---|---|
query | string |
bindValues? | 未知[] |
Promise<T>
// for sqlite & postgresconst result = await db.select( "SELECT * from todos WHERE id = $1", [ id ]);
// for mysqlconst result = await db.select( "SELECT * from todos WHERE id = ?", [ id ]);源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L141
static get(path): defaultget
一个静态初始化程序,它同步返回 Database 类的一个实例,同时将实际的数据库连接推迟到第一次调用或在数据库上进行选择时。
路径是相对于 tauri::path::BaseDirectory::App 的,并且必须以 sqlite: 开头。
| 参数 | 类型 |
|---|---|
path | string |
const db = Database.get("sqlite:test.db");源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L72
static load(path): Promise<default>load
一个静态初始化程序,它连接到基础数据库,并在建立数据库连接后返回一个 Database 实例。
路径是相对于 tauri::path::BaseDirectory::App 的,并且必须以 sqlite: 开头。
| 参数 | 类型 |
|---|---|
path | string |
const db = await Database.load("sqlite:test.db");源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L48
| 属性 | 类型 | 描述 | 定义于 |
|---|---|---|---|
lastInsertId? | 数字 | 最后插入的 id。此值不适用于 Postgres 数据库。如果 Postgres 需要最后插入的 id,则必须使用 select 函数,并带有 RETURNING 子句(例如:INSERT INTO todos (title) VALUES ($1) RETURNING id)。 | 源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L18 |
rowsAffected | 数字 | 受查询影响的行数。 | 源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L9 |
© 2026 Tauri 贡献者。CC-BY / MIT