2

開いた SQLite データベースが読み取り専用かどうかを知る必要があります。sqlite3_db_readonly() がまだない、少し古いバージョンの SQLite を使用する必要があります。sqlite3_db_readonly() の実装はプライベート呼び出しを使用するため、クライアント コードにコピーできません。ファイルを開く前に書き込み可能かどうかを確認する以外に、何ができますか?

編集: バージョンは 3.7.0.1 です。

編集 2: 私は合併を使用しています。

4

1 に答える 1

2

You probably already got your hands on the sqlite3 "Database Connection Handle" object. It is defined in sqliteInt.h:

struct sqlite3 {
  sqlite3_vfs *pVfs;            /* OS Interface */
  int nDb;                      /* Number of backends currently in use */
  Db *aDb;                      /* All backends */
  int flags;                    /* Miscellaneous flags. See below */
  int openFlags;                /* Flags passed to sqlite3_vfs.xOpen() */
  int errCode;                  /* Most recent error code (SQLITE_*) */
  int errMask;                  /* & result codes with this before returning */

You can then test for read-only with the openFlags member against O_RDWR.

#define O_RDONLY        00000000
#define O_WRONLY        00000001
#define O_RDWR          00000002

For sure this is not the same as the newer sqlite3_db_readonly(), but it might be enough in your context.

Edit

Following up on your comment you can do the following to "future-proof" your code:

  • check if the sqlite3 struct varies between 3.7.0.1 and the first version that supports sqlite3_db_readonly()
  • use some macros to map the right dupe of the sqlite3 struct (or the right offset of openFlags) with the corresponding 3.x.y.z version since SQLITE_VERSION_NUMBER is defined in sqlite3.h.
  • starting with the version that supports sqlite3_db_readonly() just invoke it.

Note that a database will be opened read-only if its file can only be opened for read, whether the sqlite3_open_v2 function is used with the SQLITE_OPEN_READONLY or not. So in addition to the aforementioned openFlags you'll also need to separately verify if the file can be opened read-only. The C API provides the function xAccess in the sqlite3_vfs struct. I'm guessing that this work whether your application already has a lock on the db file. See http://www.sqlite.org/c3ref/vfs.html for more info.

于 2012-04-15T11:33:25.293 に答える