sqlite3-入门日记2-VC环境配置

    今天学习了下sqlite3在VC6中的接口方法。要点主要如下:

一、文件准备:

1.sqlite3.h:可以有以下两种方法来放置此文件。

1)放在主机上VC6安装路径中的Include文件夹中,我的是:C:\program files\microsoft visual studio\vc98\include

2) 指定路径:打开VC执行程序,进入Tools菜单->options->directories,设定此文件放置路径。

2. sqlite3.dll:放在c:\windows\system32文件夹中,

3.sqlite3.lib:

放置方法有以下两种:

1)放在主机上VC6安装路径中的LIB文件夹中,我的是:C:\program files\microsoft visual studio\vc98\lib.

2) 指定路径: 打开VC执行程序,进入Tools菜单->options->directories,设定此文件放置路径。

3)备注:若此文件没有,需通过sqlite3.def文件编译所得,具体方法:

    将sqlite3.def文件拷贝到C:\program files\microsoft visual studio\vc98\bin。打开一个CMD程序,进入上述目录,执行命令:LIB /DEF:sqlite3.def /MACHINE:ix86;执行生自动生成sqlite3.lib 和sqlite3.exp两个文件;将生成的sqlite3.lib用上述(1)和(2)方法设定路径。

二、示例:

1 #include <stdio.h>
2
3 #include <stdlib.h>
4
5 #include "sqlite3.h"//此处不能写成<sqlite3.h>
6  
7  int main(){
8 int rc;//记录返回值,若不为0则出错,否则打开数据库成功
9 int i,nrows,ncols,tr; //nrows:记录返回数据库表的行数,ncols:记录返回表的列数
10 char*errmsg=NULL;//记录返回的错误信息。
11 char**results;//记录返回的查询结果
12 sqlite3 *db=NULL; //记录返回的数据库句柄
13 rc=sqlite3_open("demodb",&db);//打开数据库demodb,
14 if(rc) //若返回值不为0,出错
15 {
16   fprintf(stderr,"can't open db!\n",sqlite3_errmsg(db));
17   sqlite3_close(db);
18   exit(1);
19 }
20 else
21 {
22   printf("db open successfully!\n");
23
24 }
25 //下述函数进行查询数据库操作,
26 sqlite3_get_table(db,"select * from clients;",&results,&nrows,&ncols,&errmsg);
27
28 printf("DB has %d rows and %d cols\n\n",nrows,ncols);
29 tr=(nrows+1)*ncols;
30 for(i=0;i<tr;++i) //输出查询结果
31 {
32   printf("results[%d]= %s\n",i,results[i]); //此处可以自己定义输出格式,
33 }
34 sqlite3_free_table(results); //free
35 sqlite3_close(db);
36 return0;
37 }
posted @ 2011-03-04 22:10  ballwql  阅读(2441)  评论(0编辑  收藏  举报