tauri-plugin-rusqlite

Crates.iotauri-plugin-rusqlite
lib.rstauri-plugin-rusqlite
version0.4.4
sourcesrc
created_at2024-03-21 02:38:14.440739
updated_at2024-04-19 03:59:32.572552
descriptionTauri Plugin based on Rusqlite
homepage
repository
max_upload_size
id1181161
size38,859
kess (kessdev)

documentation

README

Tauri Plugin Rusqlite

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.

Example

npm run tauri dev

Installation

Rust

  • Enter the src-tauri directory 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.rs file located in the src-tauri/src directory of your project. Modify the main function 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");
}

Webview

  • Navigate to the root directory of your source code.

  • Execute the following command in your terminal:

npm i tauri-plugin-rusqlite-api

Usage

Import plugin

import Rusqlite from 'tauri-plugin-rusqlite-api'

Open database

const database = await Rusqlite.openInMemory("test.db");

or

const database = await Rusqlite.openInPath("./folder/test.db");

Init database

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);");

Drop table

await database.batch("DROP TABLE test;");

Insert or Update

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]
]));

Select

let result = await database.select("SELECT * FROM test", new Map());
...
result.map((item) => {
    return {
        id: item.id,
        ...
    };
});

Close database

await database.close();

License

MIT

Commit count: 0

cargo fmt