sqlite CC++ API接口介绍

sqlite CC++ API接口介绍,第1张

概述转载自:http://www.voidcn.com/article/p-eyinxrhb-og.html 上一篇文章sqlite之我见--简单介绍与基本 *** 作已经初步介绍了sqlite一些基本的知识与简单的 *** 作,这里我们接着介绍最重要的部分,如何将sqlite用到我们的程序中。 1. 概论 sqlite3是为了满足以下需求而开发的 1)支持UTF-16编码 2)用户自定义的文本排序方法 3)可以对B

转载自:http://www.jb51.cc/article/p-eyinxrhb-og.html

上一篇文章sqlite之我见--简单介绍与基本 *** 作已经初步介绍了sqlite一些基本的知识与简单的 *** 作,这里我们接着介绍最重要的部分,如何将sqlite用到我们的程序中。

1. 概论

sqlite3是为了满足以下需求而开发的

1)支持UTF-16编码 2)用户自定义的文本排序方法 3)可以对BLOBs字段建立索引
NOTE:sqlite3跟之前的版本数据库格式是不兼容的 最简单的程序可以用sqlite3_open(),sqlite3_exec(),sqlite3_close()3个接口来完成。 如果想更好地控制数据库引擎,可以用sqlite3_prepare()来把SQL语句编译成字节码,然后使用sqlite3_step()来执行编译后的字节码。以sqlite3_column_开头的一组API来获取查询结果集中的信息。
接下来我们先介绍一下sqlITE的一些常用API函数:

typedef struct sqlite3 sqlite3; //数据库连接对象
typedef struct sqlite3_stmt sqlite3_stmt; //预处理语句对象
2. API接口介绍 1)sqlite3_open_v2 int sqlite3_open_v2(
const char *filename,/* Database filename (UTF-8) */
sqlite3 **ppDb,/* OUT: sqlite db handle */
int flags,/* Flags */
const char *zVfs /* name of VFS module to use */
);
该接口打开与一个sqlite数据库文件的连接并返回一个数据库连接对象,相当于文件句柄。这通常是应用程序调用的第一个sqlITE API接口,而且也是调用其他sqlite API接口前需要调用的接口。许多sqlite接口需要一个指向数据库连接对象的指针作为它们的第一个参数,因而这些接口也可以理解成数据库连接对象的 *** 作接口。该接口就是创建了这样一个数据库连接对象。 Note:打开/创建数据库的命令会被缓存,直到这个数据库被真正地调用的时候才会被执行。而且允许使用PRAGMA声明来设置如本地文本编码或者默认内存页面大小等选项和参数。
sqlITE错误码:
#define sqlITE_OK 0 /* Successful result */
#define sqlITE_ERROR 1 /* sql error or missing database */
#define sqlITE_INTERNAL 2 /* An 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 sqlite_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 /* (Internal Only) table or record not found */
#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 /* (Internal Only) Database table is empty */
#define sqlITE_SCHEMA 17 /* The database schema changed */
#define sqlITE_TOOBIG 18 /* Too much data for one row of a table */
#define sqlITE_CONSTRAINT 19 /* Abort due to contraint 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_ROW 100 /* sqlite_step() has another row ready */
#define sqlITE_DONE 101 /* sqlite_step() has finished executing */

2)sqlite3_exec int sqlite3_exec(
sqlite3*,/* An open database */
const char *sql,/* sql to be evaluated */
int (*callback)(voID*,int,char**,char**),/* Callback function */
voID *,/* 1st argument to callback */
char **errmsg /* Error msg written here */
);
接口是一个对sqlite3_prepare_v2,sqlite3_step,sqlite3_finalize接口的便捷封装,以便应用程序可以用少量的代码来运行多条SQL语句。
参数解释:第1个为数据库连接对象,通过sqlite3_open得到;第2个为要实行的SQL语句,使用UTF-8编码,多条语句使用分号间隔;第3个为回调函数,对结果进行处理,第4个为传递给callback函数的第一个参数;第5个参数,当该接口执行错误的时候,可以得到错误信息。
Note:如果sqlite3_exec执行错误,errmsg有错误返回的时候,在调用sqlite3_close()之前,必须调用sqlite3_free来释放掉sqlite3_malloc()的内存。
3)sqlite3_prepare_v2 int sqlite3_prepare_v2(
sqlite3 *db,/* Database handle */
const char *zsql, /* sql statement,UTF-8 encoded */
int nByte,/* Maximum length of zsql in bytes. */
sqlite3_stmt **ppStmt,/* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zsql */
);
该接口把一个SQL语句文本转换成一个预处理语句对象并返回一个指向该对象的指针。这个接口需要之前由sqlite3_open_v2()返回的数据库连接对象指针以及一个预处理的SQL语句文本字符串作为参数。这个API并不实际解析SQL语句,仅仅是为后续的解析而对SQL语句进行的预处理。 该函数处理的SQL语句必须是UTF-8编码的,输入的参数(第2个)中,只有第一个SQL语句会被编译,而第5个参数则用来指向输入参数中下一个需要编译的SQL语句存放的sqlite statement对象的指针。
在SQL语句文本中,跟下列各式匹配的字面值可以通过下面的sqlite3_bind_*接口进行绑定。
? ?NNN :VVV @VVV $VVV 4)sqlite3_bind_ int sqlite3_bind_blob(sqlite3_stmt*,const voID*,int n,voID(*)(voID*)); int sqlite3_bind_double(sqlite3_stmt*,double); int sqlite3_bind_int(sqlite3_stmt*,int); int sqlite3_bind_int64(sqlite3_stmt*,sqlite3_int64); int sqlite3_bind_null(sqlite3_stmt*,0); Font-weight:normal; word-spacing:0px"> int sqlite3_bind_text(sqlite3_stmt*,const char*,0); Font-weight:normal; word-spacing:0px"> int sqlite3_bind_text16(sqlite3_stmt*,0); Font-weight:normal; word-spacing:0px"> int sqlite3_bind_value(sqlite3_stmt*,const sqlite3_value*); int sqlite3_bind_zeroblob(sqlite3_stmt*,int n);
以上sqlite3_bind 接口族,用来给sql声明中的通配符赋值的.没有绑定的通配符则被认为是空值.绑定上的值不会sqlite3_reset()函数重置.但是在调用了sqlite3_reset()之后所有的通配符都可以被重新赋值.
该接口族的第2个参数是被设定通配符的index值,从1开始。

5)sqlite3_step int sqlite3_step(sqlite3_stmt*); 该接口用于解析一个由先前通过sqlite3_prepare_v2()接口创建的预处理语句对象,直至返回第一行结果为止。通过再次调用sqlite3_step()可以返回下一行的结果,继续不断地调用sqlite3_step()直至整个语句完成为止。对于那些不返回结果的语句(例如INSERT,DELETE,UPDATE语句)一次调用sqlite3_step()就完成了语句的处理。 返回值:如果SQL语句执行成功则返回sqlITE_DONE, 如果sql返回了一个单行结果集,函数返回sqlITE_ROW,这种情况要调用sqlite3_column_*函数来获得记录集行中的数据。 出错,返回错误码,例如sqlITE_BUSY
6)sqlite3_column接口族 sqlite3_column()接口族,该接口返回一个由sqlite3_step()解析的预处理语句结果集中当前行的某一数据。每次执行sqlite3_step()都返回一个新的结果集中的一。可以多次调用sqlite3_column()接口返回那一行中所有列的数据。sqlite API中并没有sqlite3_column()这样的接口,取而代之的是一组用于从结果集中查询出各个列项各种数据类型数据的函数接口。在这组函数中,有些返回结果集的大小,有些返回结果集的列数。 const voID * sqlite3_column_blob(sqlite3_stmt*,int iCol);
int sqlite3_column_bytes(sqlite3_stmt*,int iCol);
int sqlite3_column_bytes16(sqlite3_stmt*,int iCol);
double sqlite3_column_double(sqlite3_stmt*,int iCol);
int sqlite3_column_int(sqlite3_stmt*,int iCol);
sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*,int iCol);
const unsigned char* sqlite3_column_text(sqlite3_stmt*,int iCol);
const voID * sqlite3_column_text16(sqlite3_stmt*,int iCol);
int sqlite3_column_type(sqlite3_stmt*,int iCol);
sqlite3_value * sqlite3_column_value(sqlite3_stmt*,int iCol);
int sqlite3_column_count(sqlite3_stmt *pStmt); int sqlite3_column_count(sqlite3_stmt *pStmt); 该接口返回结果集中包含的列数,该接口可在执行了sqlite3_prepare_v2()之后的任何时刻调用。 如果sqlite3_step()执行的是一个没有返回数据的语句,那么该接口会返回0. int sqlite3_data_count(sqlite3_stmt *pStmt); 该接口必须在sqlite3_step()之后调用,其他跟sqlite3_column_count()基本相同。 NOTE:如果sqlite3_step()返回sqlITE_DONE或者错误代码,则sqlite3_data_count会返回0,而sqlite3_column_count仍然会返回结果集包含的列数。
返回的结果集通过其他的几个sqlite3_column_***()函数来提取,所有的这些函数都把列的编号作为第2个参数,列编号从左到右,从0开始。 int sqlite3_column_type(sqlite3_stmt*,int iCol); 该接口返回第iCol列的值的数据类型。具体返回值如下: #define sqlITE_INTEGER 1 #define sqlITE_float2 #define sqlITE_TEXT 3 #define sqlITE_BLOB 4 #define sqlITE_NulL 5 NOTE:不一定非要按照该接口返回的数据类型来获取数据。数据类型不同时软件会自动转换。
7)sqlite3_reset int sqlite3_reset(sqlite3_stmt*); 该接口用来重置一个sql预处理语句对象的状态,使得它可以被再次执行。
8)sqlite3_finalize int sqlite3_finalize(sqlite3_stmt*);
该接口销毁之前调用sqlite3_prepare_v2()创建的预处理语句对象。每一预处理语句对象都必须调用这个接口进行销毁以避免内存泄露
任何时候调用该接口,都会销毁一个prepared的sql声明,在sqlite3_close()之前,必须通过该接口将所有prepared的声明释放销毁。
9)sqlite3_close int sqlite3_close(sqlite3 *); 该接口销毁之前调用sqlite3_open_v2()创建的数据库连接对象。所有与该连接相关的预处理语句对象都必须在关闭连接之前销毁,否则该接口会返回让你头疼的sqlITE_BUSY。

下一篇文章 sqlite之我见--C/C++ API接口示例中,我会用一些小程序来示例sqlite C/C++ API的使用。
总结

以上是内存溢出为你收集整理的sqlite C/C++ API接口介绍全部内容,希望文章能够帮你解决sqlite C/C++ API接口介绍所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1163484.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存