我应该使用什么版本的 rusqlite?
What version of rusqlite should I use?
我正在学习 Rust 语言。所以,我尝试使用 sqlite3 构建一个简单的网络应用程序。但是它得到多个包 link 错误。
我看到了这个错误的一些解决方案(例如 this),但它们没有用。
原因好像是rusqlite
的版本说明不对,但不知道正确的版本说明。
我应该如何配置 cargo.toml
?
源代码在这里。
cargo.toml
[package]
name = "todo"
version = "0.1.0"
edition = "2018"
[dependencies]
actix-web = "4.0.0-beta.3"
actix-rt = "2.2.0"
thiserror = "1.0.29"
askama = "0.10.5"
rusqlite = { version = "0.23", features = ["bundled"] }
r2d2 = "0.8.9"
r2d2-sqlite3 = "0.1.1"
main.rs
use actix_web::{get, App, HttpResponse, HttpServer, ResponseError};
use thiserror::Error;
use askama::Template;
use r2d2::Pool;
use r2d2_sqlite3::SqliteConnectionManager;
use rusqlite::params;
struct TodoEntry {
id: u32,
text: String,
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
entries: Vec<TodoEntry>,
}
#[derive(Error, Debug)]
enum MyError {
#[error("Failed to render HTML")]
AskamaError(#[from] askama::Error),
}
impl ResponseError for MyError {}
#[get("/")]
async fn index() -> Result<HttpResponse, MyError> {
let mut entries = Vec::new();
entries.push(TodoEntry {
id: 1,
text: "First entry".to_string(),
});
entries.push(TodoEntry {
id: 2,
text: "Second entry".to_string(),
});
let html = IndexTemplate { entries };
let response_body = html.render()?;
Ok(HttpResponse::Ok()
.content_type("text/html")
.body(response_body))
}
#[actix_rt::main]
async fn main() -> Result<(), actix_web::Error> {
let manager = SqliteConnectionManager::file("todo.db");
let pool = Pool::new(manager).expect("Failed to initialize the connection pool.");
let conn = pool
.get()
.expect("Failed to get the connection from the pool.");
conn.execute(
"CREATE TABLE IF NOT EXISTS todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL
)",
params![],
)?
.expect("Failed to create a table `todo`");
HttpServer::new(move || App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}
错误信息在这里。
error: multiple packages link to native library `sqlite3`, but a native library can be linked only once
package `libsqlite3-sys v0.18.0`
... which is depended on by `rusqlite v0.23.1`
... which is depended on by `todo v0.1.0 (/Users/******/Documents/IntelliJ project/Rust-project/todo)`
links to native library `sqlite3`
package `sqlite3-src v0.2.9`
... which is depended on by `sqlite3-sys v0.12.0`
... which is depended on by `sqlite3 v0.24.0`
... which is depended on by `r2d2-sqlite3 v0.1.1`
... which is depended on by `todo v0.1.0 (/Users/*****/Documents/IntelliJ project/Rust-project/todo)`
also links to native library `sqlite3`
您直接依赖于 rusqlite
并使用本身依赖于 rusqlite
.
的 r2d2-sqlite3
由于 rusqlite
绑定到本地库,因为消息表明您不能将两个版本的 rusqlite 链接到不同版本的 sqlite3(-sys),因此您需要确保使用相同的版本rusqlite 作为 r2d2.
如果你不打算在 Cargo 上发布,最简单的方法是将 rusqlite
的版本保留为通配符 ("*"
),这样依赖解析器就会给你任何对 r2d2-sqlite3
有用的东西。否则你需要检查你正在使用的 r2d2-sqlite3
版本并匹配它。
顺便说一下……r2d2-sqlite3 0.1.1?那好像是over 4 years old, the current version seems to be 0.18。我对 r2d2 的工作感到有点惊讶,尽管我猜它的变化相对较小(0.8.0 是 4 年前,当前是 0.8.9)。虽然我不确定 r2d2 对 sqlite3 的实用性是什么,尤其是对于“一个简单的网络应用程序”。
我正在学习 Rust 语言。所以,我尝试使用 sqlite3 构建一个简单的网络应用程序。但是它得到多个包 link 错误。
我看到了这个错误的一些解决方案(例如 this),但它们没有用。
原因好像是rusqlite
的版本说明不对,但不知道正确的版本说明。
我应该如何配置 cargo.toml
?
源代码在这里。
cargo.toml
[package]
name = "todo"
version = "0.1.0"
edition = "2018"
[dependencies]
actix-web = "4.0.0-beta.3"
actix-rt = "2.2.0"
thiserror = "1.0.29"
askama = "0.10.5"
rusqlite = { version = "0.23", features = ["bundled"] }
r2d2 = "0.8.9"
r2d2-sqlite3 = "0.1.1"
main.rs
use actix_web::{get, App, HttpResponse, HttpServer, ResponseError};
use thiserror::Error;
use askama::Template;
use r2d2::Pool;
use r2d2_sqlite3::SqliteConnectionManager;
use rusqlite::params;
struct TodoEntry {
id: u32,
text: String,
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
entries: Vec<TodoEntry>,
}
#[derive(Error, Debug)]
enum MyError {
#[error("Failed to render HTML")]
AskamaError(#[from] askama::Error),
}
impl ResponseError for MyError {}
#[get("/")]
async fn index() -> Result<HttpResponse, MyError> {
let mut entries = Vec::new();
entries.push(TodoEntry {
id: 1,
text: "First entry".to_string(),
});
entries.push(TodoEntry {
id: 2,
text: "Second entry".to_string(),
});
let html = IndexTemplate { entries };
let response_body = html.render()?;
Ok(HttpResponse::Ok()
.content_type("text/html")
.body(response_body))
}
#[actix_rt::main]
async fn main() -> Result<(), actix_web::Error> {
let manager = SqliteConnectionManager::file("todo.db");
let pool = Pool::new(manager).expect("Failed to initialize the connection pool.");
let conn = pool
.get()
.expect("Failed to get the connection from the pool.");
conn.execute(
"CREATE TABLE IF NOT EXISTS todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL
)",
params![],
)?
.expect("Failed to create a table `todo`");
HttpServer::new(move || App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}
错误信息在这里。
error: multiple packages link to native library `sqlite3`, but a native library can be linked only once
package `libsqlite3-sys v0.18.0`
... which is depended on by `rusqlite v0.23.1`
... which is depended on by `todo v0.1.0 (/Users/******/Documents/IntelliJ project/Rust-project/todo)`
links to native library `sqlite3`
package `sqlite3-src v0.2.9`
... which is depended on by `sqlite3-sys v0.12.0`
... which is depended on by `sqlite3 v0.24.0`
... which is depended on by `r2d2-sqlite3 v0.1.1`
... which is depended on by `todo v0.1.0 (/Users/*****/Documents/IntelliJ project/Rust-project/todo)`
also links to native library `sqlite3`
您直接依赖于 rusqlite
并使用本身依赖于 rusqlite
.
r2d2-sqlite3
由于 rusqlite
绑定到本地库,因为消息表明您不能将两个版本的 rusqlite 链接到不同版本的 sqlite3(-sys),因此您需要确保使用相同的版本rusqlite 作为 r2d2.
如果你不打算在 Cargo 上发布,最简单的方法是将 rusqlite
的版本保留为通配符 ("*"
),这样依赖解析器就会给你任何对 r2d2-sqlite3
有用的东西。否则你需要检查你正在使用的 r2d2-sqlite3
版本并匹配它。
顺便说一下……r2d2-sqlite3 0.1.1?那好像是over 4 years old, the current version seems to be 0.18。我对 r2d2 的工作感到有点惊讶,尽管我猜它的变化相对较小(0.8.0 是 4 年前,当前是 0.8.9)。虽然我不确定 r2d2 对 sqlite3 的实用性是什么,尤其是对于“一个简单的网络应用程序”。