如何使用rusqlite包打开与标志的连接?

时间:2018-02-16 14:59:06

标签: sqlite rust

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

  

Connection::open(path)相当于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"]

1 个答案:

答案 0 :(得分:5)

  

相当于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);
}