SQLite 的错误代码及解析

SQL定义了如下一些错误代码:


#define SQLITE_OK           0   /* Successful result */
#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_NOTICE      27   /* Notifications from sqlite3_log() */
#define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */

其中最常见的几个返回值是:

SQLite_OK,所有API执行成功都返回它。

SQLite_ROW,遍历查询时还有下一行时返回此结果。

SQLite_DONE,遍历查询至没有下一行时返回此结果。

SQLite_ERROR,数据库还没建好的时候就查询。

SQLite_BUSY,并发错误,SQLite是读写分享的,也即读共享,写独占。当两个写操作同时到达时,会报此错误。

SQLite_CONSTRAINT,主键冲突,如果设置了主键,那么插入失败会报这个错。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sqlite3_exec()函数是SQLite库中最常用的函数之一,它用于执行SQL语句并调用回调函数处理结果。下面是sqlite3_exec()函数的详细解释: 1. 头文件和函数原型 ```c #include "sqlite3.h" int sqlite3_exec(sqlite3*, const char *sql, int (*callback)(void*para,int argc,char**argv,char**argv_name), void *para,char **errmsg) ``` 2. 参数说明 - sqlite3*:SQLite数据库连接对象。 - sql:要执行的SQL语句。 - callback:回调函数,用于处理SQL语句执行结果。 - para:传递给回调函数的参数。 - errmsg:如果执行SQL语句出错,将错误信息存储在此处。 3. 回调函数 回调函数是sqlite3_exec()函数的一个重要参数,用于处理SQL语句执行结果。回调函数的原型如下: ```c int callback(void *para, int argc, char **argv, char **argv_name); ``` - para:sqlite3_exec()函数中传递给回调函数的参数。 - argc:结果集中的列数。 - argv:结果集中的一行数据。 - argv_name:结果集中每一列的列名。 回调函数返回一个整数值,用于告诉sqlite3_exec()函数是否继续执行SQL语句。如果回调函数返回0,则继续执行SQL语句;如果返回非0值,则停止执行SQL语句。 4. 示例 下面是一个使用sqlite3_exec()函数执行SQL语句的示例: ```c #include <stdio.h> #include <stdlib.h> #include <sqlite3.h> int callback(void *para, int argc, char **argv, char **argv_name) { int i; for (i = 0; i < argc; i++) { printf("%s = %s\n", argv_name[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } int main(int argc, char **argv) { sqlite3 *db; char *errmsg = 0; int ret; ret = sqlite3_open("test.db", &db); if (ret != SQLITE_OK) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; } ret = sqlite3_exec(db, "SELECT * FROM test", callback, 0, &errmsg); if (ret != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", errmsg); sqlite3_free(errmsg); } sqlite3_close(db); return 0; } ``` 以上代码打开了一个名为test.db的SQLite数据库,并执行了一条SELECT语句,将结果集传递给回调函数callback()处理。在回调函数中,我们遍历了结果集中的每一行数据,并输出了每一列的列名和值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值