3

sqlite3 C++ API を使用しています。走った後

int rc = sqlite3_exec(db, pSQL, 0, 0, 0);

rcの結果を返しますSQLITE_OK

さらに、私はテストしましたerrno != 0。の結果cout << strerror(errno) << endlは一貫しています: No such file or directory

つまり、sqlite3 には独自の「リターン コード」(SQLITE_OK) のセットがあることを知っています。しかし、sqlite3 は errno コードを正しく/一貫して使用していますか? つまり、アプリケーションの別の部分がグローバル errno 変数を使用している場合、 を呼び出すたびにそれをリセットする必要がありsqlite3_execますか?

ありがとう。


私のコードのより完全な例:

    const int errno_start = errno;
    if (errno_start > 0) {
        std::cout << "errno=" << strerror(errno) << std::endl;
        std::cout << "errno_start=" << strerror(errno_start) << std::endl;
    }
    // prepare sql statement
    const char *pSQL;
    pSQL = "CREATE TABLE "
            "IF NOT EXISTS "
            "test1"
            "("
            "id INTEGER,"
            "field VARCHAR(32),"
            "value DOUBLE,"
            "timestamp DATETIME"
            ")";

    //int rc = sqlite3_exec(db, pSQL, callback, 0, &szErrMsg);
    int rc = sqlite3_exec(db, pSQL, 0, 0, 0);
    const int errno_here1 = errno;
    if (errno_here1 > 0) {
        std::cout << "errno_start=" << strerror(errno_start) << ", errno_here1="
                << strerror(errno_here1) << std::endl;
    }
    if (rc != SQLITE_OK) {
        std::cout << "SQL Error: " << szErrMsg << std::endl;
        sqlite3_free(szErrMsg);
    } else {
        std::cout << "initialize successful" << std::endl;
    }

このスニペットの結果は次のとおりです。

errno_start=成功、errno_here1=そのようなファイルまたはディレクトリはありません

初期化成功

4

2 に答える 2

2

失敗する可能性のあるほとんどすべての OS 呼び出し (つまり、ほとんどすべて) が値を設定する可能性があるため、次の OS 呼び出しを超えて永続化するために in の値に依存するべきではありません。errnoただし、少なくとも 1 つのことがあります。errno現在は通常、舞台裏でスレッド ローカルになっています。それでも、ライブラリはerrno呼び出し時に設定される可能性があるため、システム コールが失敗した直後に値を保存し、それ以外の場合は無視するのが賢明です。

SQLite は内部で OS 呼び出しを行うため (もちろん、DB ファイルにアクセスするとき)、errno事実上何でも設定できます。発生したエラーは内部で処理されます。何か問題がある場合は、文書化された独自のエラーメカニズムを使用して通知します。これには、発生した時点での情報は含まれませんerrno(実際、これは移植性がありません。SQLite は Windows でも実行され、別のエラー コード レポート メカニズムを使用します)。

于 2013-03-26T13:22:56.887 に答える
0

sqlite3 returns an result code that corresponds to a set of DEFINES as laid out in the sqlite3 API:

#define SQLITE_OK           0   /* Successful result */
/* beginning-of-error-codes */
#define SQLITE_ERROR        1   /* SQL error or missing database */
#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
#define SQLITE_PERM         3   /* Access permission denied */
#define SQLITE_ABORT        4   /* Callback routine requested an abort */
#define SQLITE_BUSY         5   /* The database file is locked */
#define SQLITE_LOCKED       6   /* A table in the database is locked */
#define SQLITE_NOMEM        7   /* A malloc() failed */
#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
#define SQLITE_FULL        13   /* Insertion failed because database is full */
#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
#define SQLITE_EMPTY       16   /* Database is empty */
#define SQLITE_SCHEMA      17   /* The database schema changed */
#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
#define SQLITE_MISMATCH    20   /* Data type mismatch */
#define SQLITE_MISUSE      21   /* Library used incorrectly */
#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
#define SQLITE_AUTH        23   /* Authorization denied */
#define SQLITE_FORMAT      24   /* Auxiliary database format error */
#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB      26   /* File opened that is not a database file */
#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
/* end-of-error-codes */

Nothing in the API indicates that they use the same error codes as defined in Errno.h. It is simply a coincidence.

sqlite3 API already provides an interface to print a string representation of the error code. I recommend you study the API a little more.

EDIT: Based on your comment, I have looked at the source for sqlite3 and it uses OS functions which set errno. That may be why you are seeing errno change. There is no relation between SQLITE result code and errno.

于 2013-03-25T15:08:13.957 に答える