I am interested in directly using SQL queries to work with the database in a Tauri application. Here’s a code snippet illustrating what I want to achieve:
// Create or connect to the SQLite database
let database_connection = Connection::open("my_database.db").map_err(|error| error.to_string())?;
// Create a table to store session data
database_connection.execute(
"CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY,
session_name TEXT NOT NULL,
session_value TEXT NOT NULL
)",
[],
)
.map_err(|error| error.to_string())?;
// Insert session details into the database
for session in &session_list {
database_connection.execute(
"INSERT INTO sessions (session_name, session_value) VALUES (?1, ?2)",
params![session.session_name, session.session_value],
)
.map_err(|error| error.to_string())?;
}
Unfortunately, I encountered some issues while executing this method. Most resources I’ve come across utilize the tauri-plugin-sql
, which isn’t my preference for database management. Is there a way for me to use SQL directly?
I attempted to directly manipulate a complete data file like this:
use crate::types::auth::AuthData::{ResponseData, SessionData, UserSession};
use rusqlite::{params, Connection, Result as DbResult};
#[tauri::command(rename_all = "snake_case")]
pub fn authenticate(code: String) -> Result<SessionData, String> {
// Create the request URL with parameters
let mut request_url = reqwest::Url::parse("https://example.com/api/auth/poll")
.map_err(|error| error.to_string())?;
request_url.query_pairs_mut().append_pair("code", &code);
// Make a GET request using reqwest
let response = reqwest::blocking::get(request_url).map_err(|error| error.to_string())?;
let headers = response.headers();
let mut user_sessions: Vec<UserSession> = Vec::new();
// Parse all Set-Cookie headers to create UserSession objects
if let Some(_cooked_headers) = headers.get_all("Set-Cookie").iter().next() {
for cookie_header in headers.get_all("Set-Cookie") {
if let Ok(cookie_string) = cookie_header.to_str() {
if let Some((name, value)) = cookie_string.split_once('=') {
let value = value.split(';').next().unwrap_or("").to_string();
user_sessions.push(UserSession {
session_name: name.to_string(),
session_value: value,
});
}
}
}
}
// Create or connect to the SQLite database
let database_connection = Connection::open("my_database.db").map_err(|error| error.to_string())?;
// Create table for session storage
database_connection.execute(
"CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY,
session_name TEXT NOT NULL,
session_value TEXT NOT NULL
)",
[],
)
.map_err(|error| error.to_string())?;
// Insert session data into the database
for session in &user_sessions {
database_connection.execute(
"INSERT INTO sessions (session_name, session_value) VALUES (?1, ?2)",
params![session.session_name, session.session_value],
)
.map_err(|error| error.to_string())?;
}
// Check for successful response
if response.status().is_success() {
let api_response: ResponseData = response.json().map_err(|error| error.to_string())?;
let session_data = SessionData {
user_sessions,
api_response,
};
return Ok(session_data);
} else {
return Err(format!("Response failed with status: {}", response.status()).into());
}
}
During compilation, I faced the following issue:
error: linking with
link.exe
failed: exit code: 1181
I have only referenced SQL in this specific file.