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
- 用於釋放保存查詢內容的指針數組;