gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:11:24 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com