#include "stdio.h" #include "stdlib.h" extern "C" { #include "sqlite3.h" }; int open_database(sqlite3** db,char* dbName); int close_database(sqlite3* db); int query(void* para,int n_column,char** column_value,char **column_name); int main() { char* errmsg=NULL; sqlite3* db; open_database(&db,"C:\\test.db"); int ret = sqlite3_exec(db, "select * from stu;" ,query, NULL, &errmsg ); close_database(db); system("pause"); return 0; } int open_database(sqlite3** db,char* dbName) { int result; char * errmsg = NULL; result = sqlite3_open(dbName,db ); if( result != SQLITE_OK ) { // 数据库打开失败 printf("打开数据库【%s】失败\n",dbName); return 0; } printf("打开数据库【%s】成功\n",dbName); return 1; } int query_nn(sqlite3** db,char* sql) { char** dbResult; int nRow,nColumn; int index; int result; char* errmsg = NULL; // result = sqlite3_get_table(*db,sql,&dbResult,&nRow,&nColumn,&errmsg ); if( SQLITE_OK == result ) { // 查询成功 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 " ); } } //printf( "!!!错误码 :%d ,错误原因 :%s\n ", result, errmsg ); // 到这里 , 不论数据库查询是否成功 , 都释放 char** 查询结果 , 使用 sqlite 提供的功能来释放 sqlite3_free_table( dbResult ); return 1; } int close_database(sqlite3* db) { int result; char * errmsg = NULL; result = sqlite3_close(db); if( result != SQLITE_OK ) { // 数据库打开失败 printf("关闭数据库失败\n"); return 0; } printf("关闭数据库成功\n"); return 1; } int query(void* para,int n_column,char** column_value,char **column_name) { //para是你在sqlite3_exec里传入的void*参数 //n_column是这一条记录有多少个字段 //char** column_value是个关键值,查出来的数据都保存在这里(实际上是个 1 维数组) //char** column_name跟 column_value 是对应的,表示这个字段的字段名称 int i; printf("记录包含 %d 个字段\n", n_column ); for(i = 0 ; i < n_column; i++ ) { printf("字段名 :%s > 字段值 :%s\n" , column_name[i], column_value[i] ); } printf("-------------------------------\n "); return 0; } /* //创建一个测试表,表名叫stu result = sqlite3_exec( db, "create table stu( id integer primary key autoincrement, name varchar(10),age integer);" , NULL, NULL, &errmsg ); if(result != SQLITE_OK ) { printf( "创建表失败,错误码 :%d,错误原因 :%s\n" , result, errmsg ); } // 插入一些记录 result = sqlite3_exec( db, "insert into stu(name,age) values ('Alex',18);" , 0, 0, &errmsg ); if(result != SQLITE_OK ) { printf( "插入记录失败,错误码 :%d ,错误原因 :%s\n " , result, errmsg ); } result = sqlite3_exec( db, "insert into stu(name,age) values ('Bob',19);" , 0, 0, &errmsg ); if(result != SQLITE_OK ) { printf( "插入记录失败,错误码 :%d ,错误原因 :%s\n ", result, errmsg ); } result = sqlite3_exec( db, "insert into stu(name,age) values ('Cindy',20);", 0, 0, &errmsg); if(result != SQLITE_OK ) { printf( "插入记录失败,错误码 :%d ,错误原因 :%s\n " , result, errmsg ); } // 开始查询数据库 //result = sqlite3_exec( db, "select * from stu;" , LoadMyInfo, NULL, &errmsg ); */
查询结果输出到文件:
sqlite> .output ‘result’ // 指定导出文件
sqlite> select from class; // 查询数据,输出到文件
sqlite> .read ‘result’ // 查看到处的文件数据
sqlite> .output stdout // 重新设置输出终端
sqlite> select from class; // 查询数据,可在终端看到数据