After successfully installing tauri-plugin-sql by adding the specified content to src-tauri/Cargo.toml :
[dependencies.tauri-plugin-sql]
git = "https://github.com/tauri-apps/plugins-workspace"
branch = "v1"
features = ["sqlite"] # or "postgres", or "mysql"
I proceeded by adding the tauri-plugin using:
npm add https://github.com/tauri-apps/tauri-plugin-sql#v1
Next, I included the following content in main.rs :
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_sql::Builder::default().build())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Following that, I created a databaseService within the root folder of my vue js project :
import Database from "tauri-plugin-sql-api";
class DatabaseService {
constructor() {
// Initialize the database connection here if needed
}
async connectToDatabase() {
try {
// Connecting to sqlite with the path relative to `tauri::api::path::BaseDirectory::App`.
return await Database.load("sqlite:./mydata.db");
// Connection to mysql or postgres can also be established as required
} catch (error) {
console.error("Error connecting to database:", error);
throw error;
}
}
async executeQuery(query, params) {
try {
const db = await this.connectToDatabase();
return await db.execute(query, params);
} catch (error) {
console.error("Error executing query:", error);
throw error;
}
}
}
export default new DatabaseService();
Subsequently, I attempted some test queries:
<template>
<div>
<!-- Your component template -->
<h1 class="text-black text-center text-lg font-black">{{ title }}</h1>
</div>
</template>
<script>
import DatabaseService from "../services/database";
export default {
// Component options
async mounted() {
try {
// Example query
const result = await DatabaseService.executeQuery(
"SELECT * FROM barbers"
);
console.log("Query result:", result);
} catch (error) {
console.error("Error:", error);
}
},
};
</script>
<style>
/* Your component styles */
</style>
However, an error was encountered:
databaseService.js:24 Error executing query: error returned from database: (code: 1) no such table: barbers
In spite of having the table, it appears there might be an issue with the path of the sqlite file. Should it be based on src-tauri or the vue project? Where should it be created and what path should I specify in databaseservice?