跳到内容
Tauri

@tauri-apps/plugin-sql

Database

Database 类是与 SQL 插件的 Rust 端进行通信的主要接口。

new default(path): default
参数类型
pathstring

default

源码: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L29

属性类型定义于
pathstring源码: 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 表达式传递给数据库执行。

参数类型
querystring
bindValues?未知[]

Promise<QueryResult>

// for sqlite & postgres
// INSERT example
const result = await db.execute(
"INSERT into todos (id, title, status) VALUES ($1, $2, $3)",
[ todos.id, todos.title, todos.status ]
);
// UPDATE example
const result = await db.execute(
"UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
[ todos.title, todos.status, todos.id ]
);
// for mysql
// INSERT example
const result = await db.execute(
"INSERT into todos (id, title, status) VALUES (?, ?, ?)",
[ todos.id, todos.title, todos.status ]
);
// UPDATE example
const 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
参数类型
querystring
bindValues?未知[]

Promise<T>

// for sqlite & postgres
const result = await db.select(
"SELECT * from todos WHERE id = $1", [ id ]
);
// for mysql
const 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): default

get

一个静态初始化程序,它同步返回 Database 类的一个实例,同时将实际的数据库连接推迟到第一次调用或在数据库上进行选择时。

路径是相对于 tauri::path::BaseDirectory::App 的,并且必须以 sqlite: 开头。

参数类型
pathstring

default

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: 开头。

参数类型
pathstring

Promise<default>

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