sqlite3入门之sqlite3_get_table,sqlite3_free_table

sqlite3_get_table

  • sqlite3_get_table函数原型:
  • int sqlite3_get_table(
      sqlite3 *db,          /* An open database */
      const char *zSql,     /* SQL to be evaluated */
      char ***pazResult,    /* Results of the query */
      int *pnRow,           /* Number of result rows written here */
      int *pnColumn,        /* Number of result columns written here */
      char **pzErrmsg       /* Error msg written here */
    );
    void sqlite3_free_table(char **result);
  • sqlite3_get_table主要是用于非回调的方式进行select查询,参数如下;
  • 参数1:打开数据库得到的指针;
  • 参数2:一条sql语句,跟sqlite3_exec中一样;
  • 参数3:查询的数据结果,他是一个指针数组,内存分布为:字段名称,后面是紧接着是每个字段的值;
  • 参数4:查询到的数据条数,(行数);
  • 参数5:查询到的字段数,(列数);
  • 参数6:错误信息;
  • char *str_he = "";
        char *str_fen = "";
    
        //初始化表
        for(rc = 0; rc < 16; rc++) {
            sql = sqlite3_mprintf("INSERT INTO RELAY VALUES ('%d', '%q', '%q', '2019-7-12');", rc, str_he, str_fen);
            sqlite3_exec(db, sql, 0, 0, &zErrMsg);
        }
    
        rc = sqlite3_get_table(db, "SELECT  * FROM RELAY", &dbresult, &nRow, &nColum, &zErrMsg);
        if(rc == SQLITE_OK) {
            index = nColum;
            for(i = 0; i < nRow; i++) {
                for(j = 0; j < nColum; j++) {
                    printf("%d--%s : %s\n", i, dbresult[j], dbresult[index++]);
                    //  ++index;
                }
                printf("----------------------------------------------\n");
            }
        }
        sqlite3_free_table(dbresult);
  • 输出结果:
    0--ID : 0
    0--C_STATUS : 合
    0--W_STATUS : 分
    0--TIME : 2019-7-12
    ----------------------------------------------
    1--ID : 1
    1--C_STATUS : 合
    1--W_STATUS : 分
    1--TIME : 2019-7-12
    ----------------------------------------------
    2--ID : 2
    2--C_STATUS : 合
    2--W_STATUS : 分
    2--TIME : 2019-7-12
    ----------------------------------------------
    。。。。。。
    。。。。。。
    ----------------------------------------------
    14--ID : 14 14--C_STATUS : 合 14--W_STATUS : 分 14--TIME : 2019-7-12 ---------------------------------------------- 15--ID : 15 15--C_STATUS : 合 15--W_STATUS : 分 15--TIME : 2019-7-12 ----------------------------------------------
  • 从输出结果可以看出内存分布如下:

sqlite3_free_table

  • 用于释放保存查询内容的指针数组;
posted @ 2019-07-14 10:29  常瑟  阅读(9563)  评论(0编辑  收藏  举报