SQL
插件提供了一个接口,让前端可以通过 sqlx 与 SQL 数据库进行通信。它支持 SQLite、MySQL 和 PostgreSQL 驱动程序,通过 Cargo 特性来启用。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | ||
| ios |
安装 SQL 插件以开始使用。
使用项目的包管理器添加依赖:
npm run tauri add sqlyarn run tauri add sqlpnpm tauri add sqldeno task tauri add sqlbun tauri add sqlcargo tauri add sql-
在
src-tauri目录下运行以下命令,将插件添加到Cargo.toml的项目依赖中:cargo add tauri-plugin-sql -
修改
lib.rs以初始化插件:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_sql::Builder::default().build()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你偏好的 JavaScript 包管理器安装 JavaScript Guest 绑定:
npm install @tauri-apps/plugin-sqlyarn add @tauri-apps/plugin-sqlpnpm add @tauri-apps/plugin-sqldeno add npm:@tauri-apps/plugin-sqlbun add @tauri-apps/plugin-sql
安装插件后,你必须选择支持的数据库引擎。
可用的引擎包括 SQLite、MySQL 和 PostgreSQL。
在 src-tauri 目录下运行以下命令以启用你偏好的引擎:
cargo add tauri-plugin-sql --features sqlitecargo add tauri-plugin-sql --features mysqlcargo add tauri-plugin-sql --features postgres所有插件的 API 都可以通过 JavaScript Guest 绑定使用:
路径相对于 tauri::api::path::BaseDirectory::AppConfig。
import Database from '@tauri-apps/plugin-sql';// 当使用 `"withGlobalTauri": true` 时,你可以使用// const Database = window.__TAURI__.sql;
const db = await Database.load('sqlite:test.db');await db.execute('INSERT INTO ...');import Database from '@tauri-apps/plugin-sql';// 当使用 `"withGlobalTauri": true` 时,你可以使用// const Database = window.__TAURI__.sql;
const db = await Database.load('mysql://user:password@host/test');await db.execute('INSERT INTO ...');import Database from '@tauri-apps/plugin-sql';// 当使用 `"withGlobalTauri": true` 时,你可以使用// const Database = window.__TAURI__.sql;
const db = await Database.load('postgres://user:password@host/test');await db.execute('INSERT INTO ...');我们使用 sqlx 作为底层库并采用它们的查询语法。
在替换查询数据时使用 “$#” 语法
const result = await db.execute( 'INSERT into todos (id, title, status) VALUES ($1, $2, $3)', [todos.id, todos.title, todos.status]);
const result = await db.execute( 'UPDATE todos SET title = $1, status = $2 WHERE id = $3', [todos.title, todos.status, todos.id]);在替换查询数据时使用 “?” 语法
const result = await db.execute( 'INSERT into todos (id, title, status) VALUES (?, ?, ?)', [todos.id, todos.title, todos.status]);
const result = await db.execute( 'UPDATE todos SET title = ?, status = ? WHERE id = ?', [todos.title, todos.status, todos.id]);在替换查询数据时使用 “$#” 语法
const result = await db.execute( 'INSERT into todos (id, title, status) VALUES ($1, $2, $3)', [todos.id, todos.title, todos.status]);
const result = await db.execute( 'UPDATE todos SET title = $1, status = $2 WHERE id = $3', [todos.title, todos.status, todos.id]);这个插件支持数据库迁移,允许你管理数据库模式随时间的变化。
迁移在 Rust 中使用 Migration 结构体定义。每个迁移都应该包含唯一的版本号、描述、要执行的 SQL 和迁移类型(向上或向下)。
迁移示例:
use tauri_plugin_sql::{Migration, MigrationKind};
let migration = Migration { version: 1, description: "create_initial_tables", sql: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);", kind: MigrationKind::Up,};如果你想使用文件中的 SQL,可以通过 include_str! 来引入:
use tauri_plugin_sql::{Migration, MigrationKind};
let migration = Migration { version: 1, description: "create_initial_tables", sql: include_str!("../drizzle/0000_graceful_boomer.sql"), kind: MigrationKind::Up,};向插件构建器添加迁移
Section titled “向插件构建器添加迁移”迁移使用插件提供的 Builder 结构体注册。使用 add_migrations 方法将迁移添加到特定数据库连接的插件中。
添加迁移示例:
use tauri_plugin_sql::{Builder, Migration, MigrationKind};
fn main() { let migrations = vec![ // 在此定义你的迁移 Migration { version: 1, description: "create_initial_tables", sql: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);", kind: MigrationKind::Up, } ];
tauri::Builder::default() .plugin( tauri_plugin_sql::Builder::default() .add_migrations("sqlite:mydatabase.db", migrations) .build(), ) ...}要在插件初始化时应用迁移,请将连接字符串添加到 tauri.conf.json 文件中:
{ "plugins": { "sql": { "preload": ["sqlite:mydatabase.db"] } }}或者,客户端的 load() 方法也会对给定的连接字符串运行迁移:
import Database from '@tauri-apps/plugin-sql';const db = await Database.load('sqlite:mydatabase.db');确保迁移按正确的顺序定义,并且可以安全地多次运行。
- 版本控制:每个迁移必须有一个唯一的版本号。这对于确保迁移按正确的顺序应用至关重要。
- 幂等性:编写迁移时,要确保它们能够安全地重新运行,而不会导致错误或意外后果。
- 测试:彻底测试迁移,确保它们按预期工作,并且不会损害数据库的完整性。
默认情况下,所有潜在危险的插件命令和作用域都被阻止,无法访问。你必须在 capabilities 配置中修改权限以启用它们。
{ "permissions": [ ..., "sql:default", "sql:allow-execute", ]}Default Permission
Default Permissions
This permission set configures what kind of database operations are available from the sql plugin.
Granted Permissions
All reading related operations are enabled. Also allows to load or close a connection.
This default permission set includes the following:
allow-closeallow-loadallow-select
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the close command without any pre-configured scope. |
|
|
Denies the close command without any pre-configured scope. |
|
|
Enables the execute command without any pre-configured scope. |
|
|
Denies the execute command without any pre-configured scope. |
|
|
Enables the load command without any pre-configured scope. |
|
|
Denies the load command without any pre-configured scope. |
|
|
Enables the select command without any pre-configured scope. |
|
|
Denies the select command without any pre-configured scope. |
© 2026 Tauri Contributors. CC-BY / MIT