gpt4 book ai didi

c - sqlite3_open 无法识别 C 中的 URI 文件名

转载 作者:行者123 更新时间:2023-11-30 15:04:49 25 4
gpt4 key购买 nike

我编写了这个示例 C 代码来打开数据库:

#include <stdio.h>
#include <sqlite3.h>
#include "litereplica.h"

int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *uri ="file:/path-to-db/fuel_transaction_1.db";
rc = sqlite3_open(uri, &db);

if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}

当我编译并运行此代码时,输​​出为 Can't open database: unable to open database file 。我改变了char *uri ="file:/path-to-db/fuel_transaction_1.db";对此:char *uri ="/path-to-db/fuel_transaction_1.db";它打开了数据库。

有人可以告诉我为什么它无法识别 URI 文件名吗?

谢谢

最佳答案

简而言之:如果你想要 URI 支持,你必须使用 SQLITE_USE_URI=1 编译你的 sqlite 或使用此行打开你的数据库。

rc = sqlite3_open_v2(uri, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI, NULL);

来自SQLite documentation :

Backwards Compatibility

In order to maintain full backwards compatibility for legacy applications, the URI filename capability is disabled by default. URI filenames can be enabled or disabled using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. The compile-time setting for URI filenames can be changed at start-time using the sqlite3_config(SQLITE_CONFIG_URI,1) or sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. Regardless of the compile-time or start-time settings, URI filenames can be enabled for individual database connections by including the SQLITE_OPEN_URI bit in the set of bits passed as the F parameter to sqlite3_open_v2(N,P,F,V).

If URI filenames are recognized when the database connection is originally opened, then URI filenames will also be recognized on ATTACH statements. Similarly, if URI filenames are not recognized when the database connection is first opened, they will not be recognized by ATTACH.

Since SQLite always interprets any filename that does not begin with "file:" as an ordinary filename regardless of the URI setting, and because it is very unusual to have an actual file begin with "file:", it is safe for most applications to enable URI processing even if URI filenames are not currently being used.

试试这个代码:

#include <stdio.h>
#include <sqlite3.h>
#include "litereplica.h"

int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *uri ="file:/path-to-db/fuel_transaction_1.db";

rc = sqlite3_open_v2(uri, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI, NULL);

//rc = sqlite3_open(uri, &db);

if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}

关于c - sqlite3_open 无法识别 C 中的 URI 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132421/

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