Use Sqlite3 with VC6 (节选自网络,来源:)
1. 下载已经编译好的dll,def文件.
下载源码文件,要里面的sqlite3.h等头文件
2. 编译LIB(原来不知道还可以这样生成lib文件,使用VC6)
建一个bat文件:
PATH = C:\Program Files\Microsoft Visual Studio\VC98\Bin;%PATH%
PATH = C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin\IDE;%PATH%
LIB /DEF:sqlite3.def /machine:IX86
pause
执行,即得到lib文件.
3. 在VC6下使用
(将sqlite3.h和生成lib文件放到project目录里)
#include "sqlite3.h"
#pragma comment(lib, "sqlite3.lib")
static int _sql_callback(void * notused, int argc, char ** argv, char ** szColName)
{
int i;
for ( i=0; i < argc; i++ ){
printf( "%s = %s\n", szColName[i], argv[i] == 0 ? "NUL" : argv[i] );
}
return 0;
}
int main(int argc, char * argv[]){
const char * sSQL1 = "create table users(userid varchar(20) PRIMARY KEY, age int, birthday datetime);";
const char * sSQL2 = "insert into users values('wang',20,'1989-5-4');";
const char * sSQL3 = "select * from users;";
sqlite3 * db = 0;
char * pErrMsg = 0;
int ret = 0;
// 连接数据库
ret = sqlite3_open("./test.db", &db);
if ( ret != SQLITE_OK ){
fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));
return(1);
}
printf("数据库连接成功!\n");
// 执行建表SQL
sqlite3_exec( db, sSQL1, 0, 0, &pErrMsg );
if ( ret != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", pErrMsg);
sqlite3_free(pErrMsg);
}
// 执行插入记录SQL
sqlite3_exec( db, sSQL2, 0, 0, &pErrMsg);
// 查询数据表
sqlite3_exec( db, sSQL3, _sql_callback, 0, &pErrMsg);
// 关闭数据库
sqlite3_close(db);
db = 0;
return 0;
}
编译通过,运行结果:
数据库连接成功!
userid = wang
age = 20
birthday = 1989-5-4
Press any key to continue
阅读(820) | 评论(0) | 转发(0) |