如何使用 rust sqlx 创建 SQLite 数据库

cda*_*nge 10 rust rust-sqlx

我正在尝试从 rusqlite => sqlx 进行转换。

通过调用 SQLite::open 打开连接rusqlite并创建数据库文件。以下作品:

use rusqlite::Connection;

Connection::open(db_filename)
Run Code Online (Sandbox Code Playgroud)

但是,我正在关注 sqlx 方面的文档(https://github.com/launchbadge/sqlx#connecting),他们立即让我创建一个池:

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))?;
Run Code Online (Sandbox Code Playgroud)

这实际上产生了Result<_, String>这样的信息:

Error: "error returned from database: unable to open database file\nfile: /path/to/my.db"
Run Code Online (Sandbox Code Playgroud)

我不清楚如何sqlx在第一次启动时实际写入这些数据库文件。

尖端?

cda*_*nge 5

?mode=rwc我在他们的问题跟踪器中发现了一个问题,您可以在文件名上使用查询样式参数。

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

添加查询解决了问题。