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

标签 c sqlite uri

我编写了这个示例 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/

相关文章:

c - C 中的基本键盘处理程序

c - C 程序中部分正确的输出

Sqlite 按日期分组获取该日期的最新行值

android - 自己的 SQLite 数据库和 onUpgrade

java - C 和 C++ 中的 JNI 调用不同?

c - 使用 ReadFile 读取整个 PhysicalDrive 内容

iphone - 如何在 iPhone 中安全地存储 SQLite 数据库..?

Azure 资源标识符格式 URI -URL 或 URN

ruby-on-rails - 如何防止管道字符在 Rails 3/Ruby 1.9.2 中导致错误的 URI 错误?

android - 未使用 setImageURI() 在 ImageView 中设置图像