SQLite3

SQLite3 C语言API :http://www.runoob.com/sqlite/sqlite-c-cpp.html

SQLite 命令:http://www.runoob.com/sqlite/sqlite-commands.html

注:使用SQLite 命令前提是Linux环境安装了SQLite3,否则命令无效;用”sqlite3+文件名/绝对路径“ 打开数据库,进去数据库命令行。

 

SQLite3 不使用回调函数获取查询结果集:

 1 int i,j,ret,nrow,ncolumn,index;
 2 char **dbresult;
 3 
 4 ret = sqlite3_get_table(db,"select * from table",&dbresult,&nrow,&ncolumn,&errmsg);
 5 if(ret == SQLITE_OK){
 6     printf("query %i records.\n",nrow);
 7     index=ncolumn;
 8     for(i=0;i<nrow;i++){
 9         printf("[%2i]",i);
10         for(j=0;j<ncolumn;j++){
11             printf(" %s",dbresult[index]);
12             index++;
13         }
14         printf("\n");
15     }
16 }
17 sqlite3_free_table(dbresult);

sqlite3_get_table的API语法:

 1 int sqlite3_get_table(
 2   sqlite3 *db,          /* An open database */
 3   const char *zSql,     /* SQL to be evaluated */
 4   char ***pazResult,    /* Results of the query */
 5   int *pnRow,           /* Number of result rows written here */
 6   int *pnColumn,        /* Number of result columns written here */
 7   char **pzErrmsg       /* Error msg written here */
 8 );
 9 void sqlite3_free_table(char **result);
10  
11 
12 其中:
13 
14 db是sqlite3的句柄
15 zSql是要执行的sql语句
16 pazResult是执行查询操作的返回结果集
17 pnRow是记录的行数
18 pnColumn是记录的字段个数
19 pzErrmsg是错误信息

 

posted @ 2019-04-11 11:28  谁的小流浪  阅读(203)  评论(0编辑  收藏  举报