- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 postgres 数据库添加到使用柴油的火箭应用程序中。我的 main.rs
文件看起来像这样,但在 diesel::Connection
处给出错误“未为 DbConnection
实现特征 .get_result(connection)
”
#[macro_use] extern crate diesel;
extern crate dotenv;
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
use diesel::prelude::*;
use rocket_contrib::database;
use rocket_contrib::json::JsonValue;
mod models;
mod schema;
use self::models::*;
use self::schema::*;
#[database("my_db")]
struct DbConnection(diesel::PgConnection);
#[get("/")]
fn index(connection: DbConnection) -> JsonValue {
json!(all_bicycles(&connection))
}
fn create_bicycle<'a>(connection: &DbConnection, make: &'a str, model: &'a str, rider_type: &'a str, size: &'a str) -> Bicycle {
let new_bicycle = NewBicycle {
make,
model,
rider_type,
size
};
diesel::insert_into(bicycles::table)
.values(new_bicycle)
// the error is on the following line, on `connection`
.get_result(connection)
.expect("Error saving bicycle")
}
fn main() {
rocket::ignite()
.attach(DbConnection::fairing())
.mount("/", routes![index])
.launch();
}
我的
Cargo.toml
(相关部分)
[dependencies]
diesel = { version = "1.4.4", features = ["postgres"] }
dotenv = "0.15.0"
rocket = { git = "https://github.com/SergioBenitez/Rocket" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
[dependencies.rocket_contrib]
git = "https://github.com/SergioBenitez/Rocket"
default-features = false
features = ["json", "diesel_postgres_pool"]
和我的
Rocket.toml
:
[global.databases]
my_db = { url = "postgres://postgres:@localhost/bikes" }
展开后的错误如下所示:
&DbConnection
the trait bound `DbConnection: diesel::Connection` is not satisfied
the trait `diesel::Connection` is not implemented for `DbConnection`
我已经设法建立到数据库的连接,并且
diesel setup
那是成功的。我还可以添加迁移——尽管我认为它们对于这个问题是不必要的。
use rocket_contrib::databases::diesel;
行。 ,与
extern crate diesel;
冲突,所以我将数据库逻辑移到了一个新模块中 -
database.rs
.什么都没有真正改变,但新模块如下所示:
use rocket_contrib::database;
use rocket_contrib::databases::diesel;
#[database("my_db")]
pub struct DbConnection(diesel::PgConnection);
它的使用方式如下:
main.rs
// ...
mod database;
use self::database::DbConnection;
// ...
错误保持不变。
最佳答案
根据rocket您需要将连接类型取消引用为实现 diesel::connection::Connection
的某种类型的文档因为包装器类型没有实现必要的特征。因此,您需要将代码更改为以下内容:
diesel::insert_into(bicycles::table)
.values(new_bicycle)
// the error is on the following line, on `connection`
.get_result(&*connection)
.expect("Error saving bicycle")
(在将连接传递给
&*
函数之前,请注意额外的
get_result
。)
关于postgresql - 特征 `diesel::Connection` 未针对 `DbConnection` 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65818709/
我正在将我们的 DAL 更改为 async DAL。 看着: await _conn.OpenAsync().ConfigureAwait(false); 我看到有一个用于打开连接的异步方法。但是为什
这是一个艰难的过程。使用完全相同的查询字符串,完全相同的以下代码: using (var db = new SqlConnection(queryString)) { await db.Open
我在网上搜索了一段时间。但是没有找到我的问题的明确答案。在连接到数据库时我应该使用“using”还是我可以直接使用 try-catch-finally?我的意思是: 我不知道是应该在每次完成与数据库的
要连接到 SQLite 数据库,相当简单:您将驱动程序和路径传递给 SQLite 文件。 其他 DBI - 兼容的数据库后端(PostgreSQL、MySQL 等),您可以传递给 dbConnect
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Close and Dispose - which to call? 我的数据层中的许多函数不受 try-c
我有一个名为 DatabaseHelper 的类,它包装了一个 DbConnection。为 using 语句设置此类的正确方法是什么?我已经实现了 IDisposible,但我不确定应该在何时何地调
我正在研究MySQL数据。我使用 dbConnect 函数连接到数据库并使用 dbReadTable 读取表。 我的问题是:如果我开始使用 tidyr 和 dplyr 等清理数据以使其整洁,这是否会更
我需要在数据库上执行一个存储过程。这是我到目前为止得到的有效方法: protected DbProviderFactory dbProviderFactory; this.dbProviderFact
我计划编写一个可以同时适用于 SQL Server 和 MySQL 的 DataConnection 库。 由于SqlConnection和MySqlConnection都是从DbConnection
我正在尝试连接到用户输入的服务器。当服务器不存在时,我想给最终用户一个快速反馈,以便他可以更正他输入的内容。 有什么方法可以在尝试连接之前测试服务器是否存在? 谢谢 最佳答案 您的 DBConnect
我有一个可以同时从数百个线程访问的单例数据库类。每个线程都有自己的连接。由于我无法控制线程数,所以我需要限制并发连接数。我决定使用如下所示的信号量。现在,连接总是在 using block 中分配,因
这是我从数据库中选择数据的函数: public DataTable SelectDataTable(string selectStatement, string connectionStrin
我正在尝试提高复杂数据库读取操作的性能。我发现一些代码,在有限的测试中,执行速度比以前使用各种技术(包括手动调整的存储过程)的尝试快得多。它使用 Dapper,但 Dapper 并不是主要的关注点。
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this qu
我在一个应用程序中找到了这段代码 Database database = DatabaseFactory.CreateDatabase("connection string"); DbConnecti
“EntityConnection 只能用关闭的 DbConnection 构造”这是我在尝试构建提供开放连接的实体连接时遇到的问题。有一个 transactionscope 打开,我不想打开一个新连
当您调用 DbConnection.GetSchema 时,您会为其提供您感兴趣的集合的字符串名称。我在任何地方都找不到集合列表。集合列表是不可变的还是因数据库类型(orcle、sql server
我有一个传递给函数的连接字符串,我需要基于这个字符串创建一个基于 DbConnection 的对象(即 SQLConnection、OracleConnection、OLEDbConnection 等
我正在尝试开始使用 sqlite + C#。我发现 SQLite-lib 不是标准库,因此我在每个引用中添加了它。 由于我更频繁地使用这个类,所以我考虑创建一个自己的类来处理所有事情。 现在,这是我的
我有一个如下所示的单例类,用于访问数据库连接 public class DBConnection { private static volatile DBConnection instance
我是一名优秀的程序员,十分优秀!