@tauri-apps/plugin-sql
类
default
Database
Database
类作为与 sql 插件的 rust 端通信的主要接口。
构造函数
new default()
new default(path): default
参数
参数 | 类型 |
---|---|
path | string |
返回
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L29
属性
属性 | 类型 | 定义于 |
---|---|---|
path | string | Source: 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 表达式传递给数据库以执行。
参数
参数 | 类型 |
---|---|
query | string |
bindValues ? | unknown [] |
返回
示例
// 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 ]);
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 |
参数
参数 | 类型 |
---|---|
query | string |
bindValues ? | unknown [] |
返回
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 ]);
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:
开头。
参数
参数 | 类型 |
---|---|
path | string |
返回
示例
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:
开头。
参数
参数 | 类型 |
---|---|
path | string |
返回
示例
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 |
rowsAffected | number | 受查询影响的行数。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L9 |
© 2025 Tauri Contributors. CC-BY / MIT