资源 中有源代码下载
1.先下载sqlite3的源代码,编译出dll和lib
2.创建一个SqliteTest工程,然后引入sqlite3.h和sqlite3.lib
3.编写如下代码:
#include "stdafx.h"
#include <stdio.h>
#include "sqlite3/sqlite3.h"
#pragma comment (lib, "sqlite3/sqlite3.lib")
static int select_callback(void * notused, int argc, char ** values, char ** colName)
{
// 每条记录回调一次该函数,有多少条就回调多少次
int i;
for ( i=0; i < argc; i++ )
{
printf( "%s = %s/n", colName[i], values[i] == 0 ? "NUL" : values[i] );
}
return 0;
}
int main(int argc, char* argv[])
{
sqlite3* db = 0;
char* zErrMsg = 0;
int rc = 0;
rc = sqlite3_open("D://00//SqliteTest//Debug//test_sqlite.db", &db);
if (rc == SQLITE_OK)
{
const char * sSQL1 = "create table users(userid varchar(20) PRIMARY KEY, age int, birthday datetime);";
char* pErrMsg = 0;
// 执行建表SQL
//回调select-------------------------
// int ret = sqlite3_exec( db, sSQL1, 0, 0, &pErrMsg );
//
// if ( ret != SQLITE_OK )
// {
// fprintf(stderr, "SQL error: %s/n", pErrMsg);
// sqlite3_free(pErrMsg);
// }
// else
// {
// // 执行插入记录SQL
// sqlite3_exec( db, "insert into users values('张三',20,'2001-5-4');", 0, 0, &pErrMsg);
// sqlite3_exec( db, "insert into users values('李四',20,'1970-5-4');", 0, 0, &pErrMsg);
// // 查询数据表
// sqlite3_exec( db, "select * from users;", select_callback, 0, &pErrMsg);
// }
//遍历select--------------------------
int nRow, nColumn;
char** dbResult;
int result = sqlite3_get_table( db, "select * from users;", &dbResult, &nRow, &nColumn, &pErrMsg );
if( SQLITE_OK == result )
{ // 查询成功
printf( "查到 %d 条记录,ncolumn=%d /n", nRow, nColumn);
for(int i = 1; i <= nRow ; i++ )
{
printf( "第 %d 条记录 /n", i );
for(int j = 0 ; j < nColumn; j++ )
{
printf( " 字段名 :%s > 字段值 :%s/n", dbResult[j], dbResult[i*nColumn + j] );
// dbResult的字段值是连续的,从第0索引到第nColumn-1索引都是字段名称,从第nColumn索引开始,
// 后面都是字段值,它把一个二维的表(传统的行列表示法)用一个扁平的形式来表示
}
printf( "-------/n" );
}
}
sqlite3_free_table( dbResult );
sqlite3_close(db);
}
return 0;
}