gpt4 book ai didi

c - 如果尝试打开不存在的文件,sqlite3_open 不会返回错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:24:12 29 4
gpt4 key购买 nike

int main(int argc,char **argv){ 
int rc
sqlite3 *db;

rc = sqlite3_open(argv[1], &db);

printf("rc = %d\n", rc);
if(rc){
printf("Not Valid Database\n");
sqlite3_close(db);
exit(1);
}

如果我使用 ./test exsisting_database.sqlite3 运行它,它会按预期返回 0。但是使用 ./test not_exsisting_database.sqlite3 它仍然返回 0 并创建文件 not_exsisting_database.sqlite3

最佳答案

从此处找到的有关打开数据库的 SQLite 文档: https://sqlite.org/c3ref/open.html

The sqlite3_open_v2() interface works like sqlite3_open() except thatit accepts two additional parameters for additional control over thenew database connection. The flags parameter to sqlite3_open_v2() cantake one of the following three values, optionally combined with theSQLITE_OPEN_NOMUTEX, SQLITE_OPEN_FULLMUTEX, SQLITE_OPEN_SHAREDCACHE,SQLITE_OPEN_PRIVATECACHE, and/or SQLITE_OPEN_URI flags:

SQLITE_OPEN_READONLY The database is opened in read-only mode. If thedatabase does not already exist, an error is returned.

SQLITE_OPEN_READWRITE The database is opened for reading and writingif possible, or reading only if the file is write protected by theoperating system. In either case the database must already exist,otherwise an error is returned.

SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE The database is opened forreading and writing, and is created if it does not already exist.This is the behavior that is always used for sqlite3_open() and sqlite3_open16().

所以你需要使用不带 SQLITE_OPEN_CREATE 标志的 sqlite3_open_v2() 而不是 sqlite3_open()

关于c - 如果尝试打开不存在的文件,sqlite3_open 不会返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073011/

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