rustrust-sqlx

How to create a SQLite database with rust sqlx


I'm trying out a conversion from rusqlite => sqlx.

Opening a connection from rusqlite calls SQLite::open, and creates the db files. The following works:

use rusqlite::Connection;

Connection::open(db_filename)

However, I'm following the docs on the sqlx side (https://github.com/launchbadge/sqlx#connecting), and they launch me immediately into creating a pool:

use sqlx::sqlite::{SqlitePoolOptions};

SqlitePoolOptions::new()
            .max_connections(1)
            .connect(&format!("sqlite://{}", db_filename))
            .await
            .map_err(|err| format!("{}\nfile: {}", err.to_string(), db_filename))?;

which in fact yields the Result<_, String> message of:

Error: "error returned from database: unable to open database file\nfile: /path/to/my.db"

I'm not clear how in the sqlx world to actually write those db files on first boot.

Tips?


Solution

  • I found an issue in their issue tracker, where you can use ?mode=rwc query style param on the filename.

    https://github.com/launchbadge/sqlx/issues/1114#issuecomment-827815038

    Adding the query solved the problem.