Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

David Rickard
David Rickard

Posted on

     

Tauri + SQLite

For years, SQLite has been my go-to solution for storing any kind of structured local data for apps. The ability to update multiple pieces of data in a transaction has been extremely useful.

Naturally, when building my app in Tauri, I wanted to use SQLite from my Rust backend code.

A bit of poking around led toRusqlite for an ergonomic SQLite wrapper.

But there's a couple other questions to answer in order to tie it in with Tauri.

Where do you store the SQLite file?

Insidesetup() we can create an app handle and callapp_handle.path_resolver().app_data_dir(). That's the designated place to store our app data. From there we can initialize our DB file.

How do we access theConnection from a Tauri command?

Tauri has a built-in ability to store app state. We'll set up struct to hold it:

pubstructAppState{pubdb:std::sync::Mutex<Option<Connection>>,}
Enter fullscreen modeExit fullscreen mode

The Mutex protects access to the connection object. The idea is that you get theConnection, run a query on it, then release the Mutex.

Insetup() we can populate this app state with the DB connection.

letapp_state:State<AppState>=handle.state();*app_state.db.lock().unwrap()=Some(db);
Enter fullscreen modeExit fullscreen mode

We can also add a convenience trait to easily access theConnection object without jumping through theState fetch andMutex lock.

pubtraitServiceAccess{fndb<F,TResult>(&self,operation:F)->TResultwhereF:FnOnce(&Connection)->TResult;}implServiceAccessforAppHandle{fndb<F,TResult>(&self,operation:F)->TResultwhereF:FnOnce(&Connection)->TResult{letapp_state:State<AppState>=self.state();letdb_connection_guard=app_state.db.lock().unwrap();letdb=db_connection_guard.as_ref().unwrap();operation(db)}}
Enter fullscreen modeExit fullscreen mode

Inside Tauri commands we can supply aapp_handle: AppHandle argument, which will be automatically populated by the Tauri framework. From there we can call ourdb() trait method onapp_handle to do database operations. Anything returned from the closure will be passed through.

letmy_result=app_handle.db(|db:&Connection|{/* Do something with the DB connection and return a value */});
Enter fullscreen modeExit fullscreen mode

I pass theAppHandle through to any other modules that need it, so they also have access to theConnection. You can also callapp_handle.clone() in case you run into ownership issues.

Full example code

https://github.com/RandomEngy/tauri-sqlite

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
johnyepthomi profile image
JohnYepthomi
  • Joined

Tauri has a sqlite plugin at "github.com/tauri-apps/tauri-plugin...".
Could you make a post using the plugin as an update to this?

CollapseExpand
 
siirko profile image
Siirko
  • Joined

github.com/tauri-apps/plugins-work...

tauri plugin for sql haven't the feature yet to be called from backend side, hope this will be implemented soon

CollapseExpand
 
eduwr profile image
Eduardo Wronscki
👨‍💻 Passionate Brazilian software engineer | Coding explorer | Tech enthusiast | Travel & food lover 🌍🍲
  • Location
    Florianópolis, Brazil
  • Education
    Federal University of Santa Catarina
  • Work
    Backend Engineer at Clevertech
  • Joined

Exactly, this is the main reason I'm not using tauri plugin.

Nice article, it was very helpful.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromDavid Rickard

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp