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

Kağ*_*yal 1 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);
}
Run Code Online (Sandbox Code Playgroud)

我实际上想用这些标志替换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
Run Code Online (Sandbox Code Playgroud)

似乎我错过了类似的东西use rusqlite::Something;,但那是什么东西?我无法弄明白.

我有以下内容 Cargo.toml

[dependencies.rusqlite]
version = "0.13.0"
features = ["bundled"]
Run Code Online (Sandbox Code Playgroud)

She*_*ter 5

相当于 Connection::open_with_flags

你应该看看open_with_flags文档:

fn open_with_flags<P: AsRef<Path>>(
    path: P, 
    flags: OpenFlags
) -> Result<Connection>
Run Code Online (Sandbox Code Playgroud)

然后点击进入OpenFlags.这将您的标志定义为关联常量:

const SQLITE_OPEN_READ_ONLY: OpenFlags
Run Code Online (Sandbox Code Playgroud)

全部一起:

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);
}
Run Code Online (Sandbox Code Playgroud)