跳到内容
Tauri

@tauri-apps/plugin-sql

数据库

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

new default(path): default
参数类型
path字符串

default

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

属性类型定义于
path字符串来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L28

close(db?): Promise<boolean>

关闭

关闭数据库连接池。

参数类型描述
db?字符串如果您管理多个数据库,可以选择指定数据库的名称。否则,所有数据库池都将在作用域内。

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>

执行

将 SQL 表达式传递给数据库执行。

参数类型
查询字符串
绑定值?未知[]

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 查询传递给数据库执行。

类型参数
T
参数类型
查询字符串
绑定值?未知[]

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

获取

一个静态初始化器,它同步返回 Database 类的实例,同时将实际的数据库连接延迟到数据库的首次调用或选择时。

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

参数类型
path字符串

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>

加载

一个静态初始化器,它连接到基础数据库并在数据库连接建立后返回 Database 实例。

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

参数类型
path字符串

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

© 2025 Tauri 贡献者。CC-BY / MIT