#include <iostream>
#include <string>
#include <tchar.h>
#include "sqlite3\\sqlite3.h"
#pragma comment (lib, "sqlite3\\sqlite3.lib")
int main(int argc, char* argv[])
{
sqlite3 *pDataBase = NULL;
char *errorMsg = 0;
int iResult = 0;
char *pszSql = NULL;
const char* data = "Callback function called";
/* Open database */
iResult = sqlite3_open("test.db", &pDataBase);
if( iResult != SQLITE_OK ){
printf( "Can't open database: %s\n", sqlite3_errmsg(pDataBase));
return -1;
}else{
printf( "Opened database successfully\n");
}
// 非回调形式;
char** dbResult = NULL;
int nRow = 0;
int nColumn = 0;
iResult = sqlite3_get_table( pDataBase, pszSql, &dbResult, &nRow, &nColumn, &errorMsg );
if( SQLITE_OK == iResult )
{
//查询成功
int index = nColumn; //前面说过 dbResult 前面第一行数据是字段名称,从 nColumn 索引开始才是真正的数据
printf("查到%d条记录\n", nRow );
for(int i = 0; i < nRow ; i++ )
{
printf("第 %d 条记录\n", i+1 );
for(int j = 0 ; j < nColumn; j++ )
{
printf("字段名:%s —— 字段值:%s\n", dbResult[j], dbResult [index] );
++index; // dbResult 的字段值是连续的,从第0索引到第 nColumn - 1索引都是字段名称,从第 nColumn 索引开始,后面都是字段值,它把一个二维的表(传统的行列表示法)用一个扁平的形式来表示
}
printf("-------\n");
}
}
//到这里,不论数据库查询是否成功,都释放 char** 查询结果,使用 sqlite 提供的功能来释放
sqlite3_free_table( dbResult );
if( iResult != SQLITE_OK ){
printf( "SQL error: %s\n", errorMsg);
sqlite3_free(errorMsg);
}else{
printf( "Operation done successfully\n");
}
sqlite3_close(pDataBase);
return 0;
}
sql/sqlite/sqlite3 表中选择数据(非回调形式)
最新推荐文章于 2021-08-15 22:04:17 发布