This plugin enables access to an SQLite database within Tauri applications. It is inspired by the tauri-plugin-sqlite plugin but is based on rusqlite.
- Full example at: https://github.com/kessdev/tauri-plugin-rusqlite/tree/main/examples/rusqlite-demo
- Execute the following command in your terminal:
npm run tauri dev-
Enter the
src-tauridirectory in your project's structure. -
Execute the following command to add tauri-plugin-rusqlite to your project's dependencies:
cargo add tauri-plugin-rusqlite- Open the
main.rsfile located in thesrc-tauri/srcdirectory of your project. Modify themainfunction to include the plugin initialization code.
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_rusqlite::init()) // add this
.run(tauri::generate_context!())
.expect("error while running tauri application");
}-
Navigate to the root directory of your source code.
-
Execute the following command in your terminal:
npm i tauri-plugin-rusqlite-apiimport Rusqlite from 'tauri-plugin-rusqlite-api'const database = await Rusqlite.openInMemory("test.db");or
const database = await Rusqlite.openInPath("./folder/test.db");let scripts = [{
name: "create_table",
sql: "CREATE TABLE test (id INTEGER PRIMARY KEY, integer_value INTEGER, real_value REAL, text_value TEXT, blob_value BLOB); CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);"
}];
await database.migration(scripts);or
await database.batch(
"CREATE TABLE test (id INTEGER PRIMARY KEY, integer_value INTEGER, real_value REAL, text_value TEXT, blob_value BLOB); CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);");await database.batch("DROP TABLE test;");await database.update("INSERT INTO test (integer_value, real_value, text_value, blob_value) VALUES (:integer_value, :real_value, :text_value, :blob_value)",
new Map([
[":integer_value", parseInt(target.integer_value.value)],
[":real_value", parseFloat(target.real_value.value)],
[":text_value", target.text_value.value],
[":blob_value", target.blob_value.value]
]));let result = await database.select("SELECT * FROM test", new Map());
...
result.map((item) => {
return {
id: item.id,
...
};
});await database.close();