首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我应该使用什么版本的rusqlite?

我应该使用什么版本的rusqlite?
EN

Stack Overflow用户
提问于 2021-10-01 09:22:55
回答 1查看 348关注 0票数 0

我在学习生锈的语言。因此,我尝试使用sqlite3构建一个简单的web应用程序。但是它会得到多个包链接错误。

我看到了一些解决这个错误的方法(例如。( ),但他们没有工作。

原因似乎是rusqlite的版本规范是错误的,但我不知道正确的版本规范。

我应该如何配置cargo.toml

源代码在这里。

cargo.toml

代码语言:javascript
复制
[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

代码语言:javascript
复制
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(())
}

错误信息就在这里。

代码语言:javascript
复制
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`
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-01 10:46:51

您直接依赖于rusqlite,使用的是r2d2-sqlite3,它本身依赖于rusqlite

由于rusqlite绑定到本机库,因为消息表明您不能有两个版本的rusqlite链接到不同版本的sqlite3(-sys),因此您需要确保使用与r2d2相同的rusqlite版本。

如果您不打算在Cargo上发布,最简单的方法是将rusqlite的版本保留为通配符("*"),这样依赖解析器就可以为您提供适用于r2d2-sqlite3的任何功能。否则,您需要检查正在使用的r2d2-sqlite3版本并与其匹配。

顺便提一下..。R2D2-平方米3,0.1.1?这似乎是4岁以上当前版本似乎是0.18。我对r2d2的工作方式有点惊讶,尽管我认为它的变化相对较小(4年前为0.8.0,当前为0.8.9)。虽然我不知道r2d2对sqlite3的用处是什么,特别是对于“一个简单的web应用程序”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69403524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档