使用sqlite3的API接口 需要加头文件 #include
编译时,需要连接sqlite3的库 -lsqlite3
1.打开数据库文件的函数
int sqlite3_open(const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */ );
功能:
打开一个数据库文件
如果存在就直接打开 不存在就新建并打开
参数:
filename 数据库文件名
ppdb 操作数据库的指针,句柄。
返回值:
成功 SQLITE_OK
失败 错误码
2.获取错误信息的函数
const char *sqlite3_errmsg(sqlite3* db);
功能:获取错误信息描述 最后一次出错的信息
3.关闭数据库文件的函数
int sqlite3_close(sqlite3* db);
功能:关闭一个数据库
4.执行sql语句的函数
int sqlite3_exec(sqlite3* db, const char *sql,
int (*callback)(void*,int,char**,char**),
void *arg, char **errmsg);
功能:
执行一条sql语句
参数:
db 数据库的句柄指针
sql 将要被执行sql语句
callback 回调函数,只有在查询语句时,才使用
arg 给回调函数callback 传参的 只有在查询语句时才给回调函数传参
errmsg 错误信息的地址
注意,如果使用了最后一个参数,
使用完之后需要手动调用sqlite3_free释放空间
返回值:
成功 SQLITE_OK
出错 错误码
----------------关于sqlite3_exec的回调函数--------------------
int (*callback)(void* arg, int ncolumn, char** f_value,char** f_name)
功能
得到查询结果
参数:
arg 为回调函数传递参数使用的
ncolumn 记录中包含的字段的数目
f_value 指向包含每个字段值的指针数组的指针
f_name 指向包含每个字段名称的指针数组的指针
返回值:
成功 0
出错 非0
5.查询数据库的函数
int sqlite3_get_table( sqlite3 *db, const char *zSql, char ***pazResult,
int *pnRow, int *pnColumn, char **pzErrmsg);//PS:要配合void sqlite3_free_table(char **result);
功能:
查询数据库,它会创建一个新的内存区域来存放查询的结果信息
不再使用结果集时 需要调用 sqlite3_free_table 来释放
参数:
db 数据库操作句柄
sql 数据库的sql语句
azResult 查询的结果
nRow 不包含表头的行数
nColumn 列数
errmsg 错误消息如果使用了 及得使用 sqlite3_free 释放
返回值:
成功 0
出错 errcode
6.释放sqlite3_get_table产生的结果集
void sqlite3_free_table(char **result);
错误码解释
#define SQLITE_OK 0 /* 成功 | Successful result */
#define SQLITE_ERROR 1 /* SQL错误 或 丢失数据库 | SQL error or missing database */
#define SQLITE_INTERNAL 2 /* SQLite 内部逻辑错误 | 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 /* 某次 malloc() 函数调用失败 | A malloc() failed */
#define SQLITE_READONLY 8 /* 尝试写入一个只读数据库 | Attempt to write a readonly database */
#define SQLITE_INTERRUPT 9 /* 操作被 sqlite3_interupt() 函数中断 | Operation terminated by ite3_interrupt() */
#define SQLITE_IOERR 10 /* 发生某些磁盘 I/O 错误 | Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT 11 /* 数据库磁盘映像不正确 | The database disk image is malformed */
#define SQLITE_NOTFOUND 12 /* sqlite3_file_control() 中出现未知操作数 | Unknown opcode in ite3_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 /* 传递给sqlite3_bind()的第二个参数超出范围 | 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() 已经产生一个行结果 | sqlite3_step() has another row ready */
#define SQLITE_DONE 101 /* sqlite3_step() 完成执行操作 | sqlite3_step() has finished executing */