sqlite - 如何使用 rusqlite crate 打开带有标志的连接?

标签 sqlite rust

我在 rusqlite 中阅读了以下内容文档:

Connection::open(path) is equivalent to Connection::open_with_flags(path, SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE).

我将其复制到以下简单代码中:

extern crate rusqlite;
use rusqlite::Connection;

fn main() {
    let path = "/usr/local/data/mydb.sqlite";
    let conn = Connection::open_with_flags(path, SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE);
}

我实际上想用 SQLITE_OPEN_READ_ONLY 替换这些标志,但认为这是一个很好的起点。

我收到以下错误:

error[E0425]: cannot find value `SQLITE_OPEN_READ_WRITE` in this scope
 --> src/main.rs:6:50
  |
6 |     let conn = Connection::open_with_flags(path, SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE);
  |                                                  ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `SQLITE_OPEN_CREATE` in this scope
 --> src/main.rs:6:75
  |
6 |     let conn = Connection::open_with_flags(path, SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE);
  |                                                                           ^^^^^^^^^^^^^^^^^^ not found in this scope

似乎我缺少类似 use rusqlite::Something; 的东西,但那是什么东西?我想不通。

我的 Cargo.toml

中有以下内容
[dependencies.rusqlite]
version = "0.13.0"
features = ["bundled"]

最佳答案

is equivalent to Connection::open_with_flags

你应该看看 open_with_flags documentation :

fn open_with_flags<P: AsRef<Path>>(
    path: P, 
    flags: OpenFlags
) -> Result<Connection>

然后点击进入 OpenFlags .这定义了 your flag作为关联常量:

const SQLITE_OPEN_READ_ONLY: OpenFlags

一起:

extern crate rusqlite;

use rusqlite::{Connection, OpenFlags};

fn main() {
    let path = "/usr/local/data/mydb.sqlite";
    let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY);
}

关于sqlite - 如何使用 rusqlite crate 打开带有标志的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829416/

相关文章:

pointers - 当 Vec 被 move 时,我可以(不安全地)持有一个指向 Vec 元素的指针吗?

c# - 使用linq构建多个where子句

sql-server - 事务中的多个参数化的Delphi SQL更新

php - PHP sql中的跨服务器连接

rust - 什么是 'core::kinds::Sized` 没有为 rust 中的 `Self' 类型实现?

time - 使用 Cargo 获取执行时间

java - 创建数据库时将数据添加到sqlite数据库

SQLite,按时间戳对数据库进行排序

c++ - 是否可以将此 Rust 代码写入语义上等效的 C++ 代码?

generics - 为什么Iterator <Item = T>和Iterator <Item =&T>的实现有冲突?