跳到内容
Tauri

@tauri-apps/plugin-sql

default

Database

Database 类作为与 sql 插件的 rust 端通信的主要接口。

构造函数

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

default

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

属性

属性类型定义于
pathstringSource: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L28

方法

close()
close(db?): Promise<boolean>

close

关闭数据库连接池。

参数
参数类型描述
db?string如果您管理多个数据库,可以选择性地声明数据库的名称。否则,所有数据库池都将在作用域内。
返回

Promise<boolean>

示例
const success = await db.close()

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L162

execute()
execute(query, bindValues?): Promise<QueryResult>

execute

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

参数
参数类型
querystring
bindValues?unknown[]
返回

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 ]
);

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L108

select()
select<T>(query, bindValues?): Promise<T>

select

将 SELECT 查询传递到数据库以执行。

类型参数
类型参数
T
参数
参数类型
querystring
bindValues?unknown[]
返回

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 ]
);

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L141

get()
static get(path): default

get

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

Sqlite

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

参数
参数类型
pathstring
返回

default

示例
const db = Database.get("sqlite:test.db");

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L72

load()
static load(path): Promise<default>

load

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

Sqlite

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

参数
参数类型
pathstring
返回

Promise<default>

示例
const db = await Database.load("sqlite:test.db");

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L48

接口

QueryResult

属性

属性类型描述定义于
lastInsertId?number最后插入的 id。此值未为 Postgres 数据库设置。如果 Postgres 上需要最后插入的 id,则必须使用 select 函数,并带有 RETURNING 子句(INSERT INTO todos (title) VALUES ($1) RETURNING id)。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L18
rowsAffectednumber受查询影响的行数。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L9

© 2025 Tauri Contributors. CC-BY / MIT