@tauri-apps/plugin-sql
类
默认
数据库
Database
类作为与SQL插件rust端通信的主要接口。
构造函数
new 默认()
new 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()
close(db?): Promise<boolean>
close
关闭数据库连接池。
参数
参数 | 类型 | 描述 |
---|---|---|
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()
execute(query, bindValues?): Promise<QueryResult>
execute
将SQL表达式传递给数据库执行。
参数
参数 | 类型 |
---|---|
查询 | 字符串 |
绑定的值 ? | 未知 [] |
返回
Promise
<QueryResult>
示例
// 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()
select<T>(query, bindValues?): Promise<T>
select
将SELECT查询传递给数据库执行。
类型参数
类型参数 |
---|
T |
参数
参数 | 类型 |
---|---|
查询 | 字符串 |
绑定的值 ? | 未知 [] |
返回
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
get()
static get(path): default
get
一个静态初始化器,在首次调用或选择数据库时,同步返回Database类的实例,同时延迟真实的数据库连接。
Sqlite
路径相对于tauri::path::BaseDirectory::App
,并且必须以sqlite:
开始。
参数
参数 | 类型 |
---|---|
路径 | 字符串 |
返回
示例
const db = Database.get("sqlite:test.db");
来源: 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:
开始。
参数
参数 | 类型 |
---|---|
路径 | 字符串 |
返回
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
接口
QueryResult
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
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