@tauri-apps/plugin-sql
此内容尚不支持你的语言。
Interface with SQL databases through sqlx. Which database engines can be used depends on the drivers enabled on the Rust side of the plugin: SQLite, MySQL and PostgreSQL.
Classes
Section titled “Classes”default
Section titled “default”Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L40
Database
The Database class serves as the primary interface for
communicating with the rust side of the sql plugin.
2.0.0
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new default(path): default;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L61
Creates a Database instance for the given connection string without
opening a connection to it. Use Database.load to connect to the
database, or Database.get for a database that is already loaded.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
path |
string |
The database connection string, such as sqlite:test.db. |
Returns
Section titled “Returns”Example
Section titled “Example”import Database from '@tauri-apps/plugin-sql'const db = new Database('sqlite:test.db')Properties
Section titled “Properties”| Property | Type | Description | Defined in |
|---|---|---|---|
path |
string |
The connection string identifying the database on the Rust side, for instance sqlite:test.db, mysql://user:pass@host/database or postgres://user:pass@host/database. |
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L46 |
Methods
Section titled “Methods”close()
Section titled “close()”close(db?): Promise<boolean>;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L220
close
Closes the database connection pool.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
db? |
string |
Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope. |
Returns
Section titled “Returns”Promise<boolean>
A promise resolving to true once the matching connection pools have been closed.
Example
Section titled “Example”import Database from '@tauri-apps/plugin-sql'const db = await Database.load('sqlite:test.db')const success = await db.close()execute()
Section titled “execute()”execute(query, bindValues?): Promise<QueryResult>;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L155
execute
Passes a SQL expression to the database for execution.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
query |
string |
The SQL statement to run, using $1, $2, … placeholders on SQLite and PostgreSQL and ? placeholders on MySQL. |
bindValues? |
unknown[] |
The values bound to the query placeholders, in the order they appear in the statement. Defaults to no values. |
Returns
Section titled “Returns”A promise resolving to the number of rows affected by the statement and the last inserted id.
Example
Section titled “Example”import Database from '@tauri-apps/plugin-sql'const db = await Database.load('sqlite:test.db')
// 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 ]);select()
Section titled “select()”select<T>(query, bindValues?): Promise<T>;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L195
select
Passes in a SELECT query to the database for execution.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
query |
string |
The SQL query to run, using $1, $2, … placeholders on SQLite and PostgreSQL and ? placeholders on MySQL. |
bindValues? |
unknown[] |
The values bound to the query placeholders, in the order they appear in the query. Defaults to no values. |
Returns
Section titled “Returns”Promise<T>
A promise resolving to the selected rows, each row being an object keyed by column name.
Example
Section titled “Example”import Database from '@tauri-apps/plugin-sql'const db = await Database.load('sqlite:test.db')
// 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 ]);static get(path): default;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L112
get
A static initializer which synchronously returns an instance of the Database class while deferring the actual database connection until the first invocation or selection on the database.
Sqlite
Section titled “Sqlite”The path is relative to tauri::path::BaseDirectory::App and must start with sqlite:.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
path |
string |
The database connection string, such as sqlite:test.db. |
Returns
Section titled “Returns”A Database instance bound to the given connection string.
Example
Section titled “Example”import Database from '@tauri-apps/plugin-sql'const db = Database.get('sqlite:test.db')load()
Section titled “load()”static load(path): Promise<default>;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L84
load
A static initializer which connects to the underlying database and
returns a Database instance once a connection to the database is established.
Sqlite
Section titled “Sqlite”The path is relative to tauri::path::BaseDirectory::App and must start with sqlite:.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
path |
string |
The database connection string, such as sqlite:test.db. The database is created if it does not exist yet, and any migration registered for it on the Rust side is run. |
Returns
Section titled “Returns”A promise resolving to a Database instance connected to the given database.
Example
Section titled “Example”import Database from '@tauri-apps/plugin-sql'const db = await Database.load('sqlite:test.db')Interfaces
Section titled “Interfaces”QueryResult
Section titled “QueryResult”Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L18
The outcome of a statement run through Database.execute.
Properties
Section titled “Properties”| Property | Type | Description | Defined in |
|---|---|---|---|
lastInsertId? |
number |
The last inserted id. This value is not set for Postgres databases. If the last inserted id is required on Postgres, the select function must be used, with a RETURNING clause (INSERT INTO todos (title) VALUES ($1) RETURNING id). |
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L29 |
rowsAffected |
number |
The number of rows affected by the query. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/sql/guest-js/index.ts#L20 |
© 2026 Tauri Contributors. CC-BY / MIT