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);
官网不推荐使用这个API,官网不推荐使用这个API,官网不推荐使用这个API。
使用该API可以得到一个结果表。结果表包含全部的查询结果,是一个含有指针的数组。
数组地址由azResult得到, 我们传入的是&azResult 。
每个指针指向一个查询数据(字符串形式)。
例子(官网):
查询到的结果如下。
Name | Age
-----------------------
Alice | 43
Bob | 28
Cindy | 21
azResult 如下。
azResult[0] = "Name"; azResult[1] = "Age"; azResult[2] = "Alice"; azResult[3] = "43"; azResult[4] = "Bob"; azResult[5] = "28"; azResult[6] = "Cindy"; azResult[7] = "21";
查询结果的行数由nRow得到,我们传入的是&nRow 。(nRow = 3)
查询结果的列数由nColumn得到,------------------------。(nColumn = 2)
注意注意注意
用完之后需要使用 sqlite3_free_table 释放 azResult 上 的内存。
用完之后需要使用 sqlite3_free_table 释放 azResult 上 的内存。
用完之后需要使用 sqlite3_free_table 释放 azResult 上 的内存。
返回
成功执行返回 SQLITE_OK
测试实例
#include <iostream>
#include <stdio.h>
#include "sqlite3.h"
using namespace std;
int main()
{
sqlite3 * db;
char **azResult;
int nRow, nColumn;
int rv = sqlite3_open("./test.db", &db);
if(rv != SQLITE_OK){
fprintf(stderr, "db can't openn");
return -1;
}
rv = sqlite3_get_table(db, "select * from user", &azResult, &nRow, &nColumn, nullptr);
if(rv != SQLITE_OK) {
fprintf(stderr, "func failedn");
return -2;
}
int left = 0;
for(int i = 0; i < nRow + 1; i++){
for(int j = 0; j < nColumn; j++){
printf("%sttt", azResult[left++]);
}
cout << endl;
}
sqlite3_free_table(azResult);
sqlite3_close(db);
return 0;
}
Name | Age ----------------------- Alice | 43 Bob | 28 Cindy | 21
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!