Abubakr Shomirsaidov
Abubakr Shomirsaidov

Reputation: 25

Failed to connect a sqlite file in tauri + vue project?

I have installed tauri-plugin-sql by adding the following 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"

then i added tauri-plugin

npm add https://github.com/tauri-apps/tauri-plugin-sql#v1

added the following content to main.rs :

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_sql::Builder::default().build())
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

then i have created an databaseService in my vue js project root folder :

import Database from "tauri-plugin-sql-api";

class DatabaseService {
  constructor() {
    // Initialize the database connection here if needed
  }

  async connectToDatabase() {
    try {
      // sqlite. The path is relative to `tauri::api::path::BaseDirectory::App`.
      return await Database.load("sqlite:./mydata.db");
      // You can also connect to mysql sor postgres as needed
    } 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();

and then i tried to do 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>
  

But here i got that error :

databaseService.js:24 Error executing query: error returned from database: (code: 1) no such table: barbers

but I swear I have that table

It seems like the problem is in the path the sqlite file . should it be accroding to src-tauri or the vue project . where should i create it and what path should i mention in databaseservice ?

Upvotes: 1

Views: 637

Answers (1)

Fraser
Fraser

Reputation: 17094

The path to the SQLite file should be specified according to where it will be located in your Tauri application, not in your Vue.js project.

As for the exact issue, it could be that whilst your mydata.db contains the barbers table - the path in your databaseService code isn't actually pointing at the same mydata.db file

Do you possibly have more than one mydata.db file in your project? I only ask as it is odd you are not hitting Error connecting to database.

That is, it appears you are successfully connecting to a db file, but the db you are connecting to doesn't contain the barbers table. If this makes sense, double check that you have a single mydata.db file and that the path to is correct relative your tauri rather than vue files.

Upvotes: 0

Related Questions