Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:sqlite3是否使用错误代码?_C++_Sqlite_Errno - Fatal编程技术网

C++ C++;:sqlite3是否使用错误代码?

C++ C++;:sqlite3是否使用错误代码?,c++,sqlite,errno,C++,Sqlite,Errno,我使用的是SqLITE3C++接口。跑步后 int rc=sqlite3_exec(db,pSQL,0,0,0) 它返回SQLITE\u OK的rc结果 此外,我还测试了errno!=0。 coutsqlite3的结果返回一个结果代码,该结果代码对应于一组定义,如下所示: API中没有任何内容表明它们使用与Errno.h中定义的相同的错误代码。这完全是巧合 已提供打印错误代码字符串表示形式的接口。我建议您多研究一下API 编辑:根据您的评论,我已经查看了,它使用的操作系统函数设置了errno。这

我使用的是SqLITE3C++接口。跑步后

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

它返回
SQLITE\u OK
rc
结果

此外,我还测试了
errno!=0

coutsqlite3的结果返回一个结果代码,该结果代码对应于一组定义,如下所示:

API中没有任何内容表明它们使用与Errno.h中定义的相同的错误代码。这完全是巧合

已提供打印错误代码字符串表示形式的接口。我建议您多研究一下API

编辑:根据您的评论,我已经查看了,它使用的操作系统函数设置了
errno
。这可能就是您看到
errno
更改的原因

您永远不应该依赖
errno
中的值在下一次操作系统调用之后保持,因为几乎任何可能失败的操作系统调用(即几乎所有操作系统调用)都可能会设置该值。但至少有一件事:
errno
现在通常是幕后的本地线程。尽管如此,当您调用库时,库可能会设置
errno
,因此明智的做法是在系统调用失败后立即保存该值,否则忽略它


SQLite在自身内部进行操作系统调用(当然,当它访问DB文件时),因此它可以很好地将
errno
设置为几乎任何内容。它遇到的错误在内部处理;如果出现错误,它会使用自己的、有文档记录的错误机制告诉您。这从不包括来自
errno
的任何信息(事实上,这是不可移植的;SQLite也在Windows上运行,并且使用不同的错误代码报告机制)。

我知道sqlite3返回自己的一组错误代码。在我的示例中,我将测试结果是否等于SQLITE_OK。但是,sqlite3_exec也在更改全局errno变量。我试图理解sqlite使用errno是否是有意的。原因是我的程序的其他部分使用了errno,我想知道调用sqlite3_exec后是否可以安全地将errno重置为0。@kmccoy:好的,但是如果你把它放在你的问题中,我的答案会有所不同。鉴于你目前的评论,你的问题目前的形式并不清楚。我感谢你的评论。我编辑了我的问题,希望能让它更清楚。谢谢。这肯定是有记录的。如果不是,那么您不应该在任何SQLite调用之后依赖
errno
的值;它很可能是由SQLite调用的函数设置的。这通常是一个很好的建议。您调用的任何库函数都可能正在执行设置errno的系统调用,库不负责记录此错误或将errno重置为0。本例中的示例:当使用CREATETABLE IF NOT EXISTS创建新表时,从sqlite3_exec返回后可以找到errno,但这与调用者无关,因此没有文档记录。
    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;
    }
#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 */