一、sqlite下载
sqlite可以在sqlite官网http://www.sqlite.org/download.html下载sqlite-dll-win32-x86-3070600.zip(这个时候发现压缩包里只有dll和def文件。)
1、将sqlite-dll-win32-x86-3070600.zip下载到自己的本地电脑上以后,(假设存储地址:E:/test);
2、打开LIB.EXE所在的目录,VC6.0下的LIB.EXE一般都存放在C:/Program Files/Microsoft Visual Studio/VC98/Bin下;
3、打开“运行”—>”cmd”进入DOS下,使用CD更改到LIB.EXE所在的当前路径,如:cdC:/Program Files/Microsoft Visual Studio/VC98/Bin
4、在DOS下输入LIB /out:E:/test/sqlite3.lib /MACHINE:IX86 /DEF:E:/test/sqlite3.def
5、回车之后,打开E:/test文件就会发现多了两个文件sqlite3.lib和sqlite3.exp
注:
1.
生成时可能会报错:
在C:\Program Files\Microsoft Visual Studio 8
搜索 MSPDB80
将其copy->C:\Program Files\Microsoft Visual Studio 8\VC\bin
2.
我生成的
sqlite3.lib
sqlite3.exp
下载地址:
@L_301_1@
3.
#include<Stdio.h>
#include"sqlite3.h"
#pragmacomment(lib,"sqlite3.lib")
intmain(intargc,char* argv[] )
{
intnRet;
sqlite3 *db=NULL;
nRet = sqlite3_open("xx.db",&db );
if( 0 != nRet )
{
printf("Open db error %s\n",sqlite3_errmsg(db));
sqlite3_close( db );
}else
{
printf("open db success\n");
}
//create a table
char*zErrMsg = 0;
char*sql =" CREATE TABLE SensorData( ID INTEGER PRIMARY KEY,SensorID INTEGER,SiteNum INTEGER,Time VARCHAR(12),SensorParameter REAL);";
printf("%s\n",sql );
nRet = sqlite3_exec( db,sql,&zErrMsg );
if( 0 != nRet )
{
printf("Create table falure,MSG:%s\n",zErrMsg);
}else
{
printf("Create table success\n");
}
//insert data
sql ="INSERT INTO \"SensorData\" VALUES(NULL,1,'200605011206',18.9 );";
nRet = sqlite3_exec( db,&zErrMsg );
if( 0 != nRet )
{
printf("Insert Error:%s",zErrMsg);
}else
{
printf("Insert OK\n");
}
//insert data
sql ="INSERT INTO \"SensorData\" VALUES(NULL,'200605011306',16.4 );";
nRet = sqlite3_exec( db,zErrMsg);
}else
{
printf("Insert OK\n");
}
//update data
sql ="UPDATE SensorData SET SensorParameter=1000.01,SensorID=123 WHERE SensorParameter=100.01";
nRet = sqlite3_exec( db,&zErrMsg );
if( 0 != nRet )
{
printf("Update Error:%s",zErrMsg);
}else
{
printf("Update OK\n");
}
//delete data
sql ="DELETE FROM SensorData WHERE SensorID=123";
nRet = sqlite3_exec( db,&zErrMsg );
if( 0 != nRet )
{
printf("Delete Error:%s",zErrMsg);
}else
{
printf("Delete OK\n");
}
//query data
intnrow = 0,ncolumn = 0;
char**azResult;//二维数组存放結果
sql ="SELECT * FROM SensorData ";
sqlite3_get_table( db,&azResult,&nrow,&ncolumn,&zErrMsg );
inti = 0 ;
printf("row:%d column=%d \n",nrow,ncolumn );
printf("\nThe result of querying is : \n");
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
printf("azResult[%d] = %s\n",i,azResult[i] );
//释放azResult的內存空間
sqlite3_free_table( azResult );
sqlite3_close( db );
return0;
}
注:请将您的sqlite3.h、sqlite3.lib、sqlite3.dll放到 你的工程目录下!
sqlite3.h ->C:\Program Files\Microsoft Visual Studio 8\VC\include
sqlite3.lib ->C:\Program Files\Microsoft Visual Studio 8\VC\lib
sqlite3.dll ->C:\Program Files\Microsoft Visual Studio 8\VC\bin
4.
执行本程序后其输出结果如下:
open db success
CREATE TABLE SensorData( ID INTEGER PRIMARY KEY,SensorParameter REAL);
Create table success
Insert OK
Insert OK
Update OK
Delete OK
row:2 column=5
The result of querying is :
azResult[0] = ID
azResult[1] = SensorID
azResult[2] = SiteNum
azResult[3] = Time
azResult[4] = SensorParameter
azResult[5] = 1
azResult[6] = 1
azResult[7] = 1
azResult[8] = 200605011206
azResult[9] = 18.9
azResult[10] = 2
azResult[11] = 1
azResult[12] = 1
azResult[13] = 200605011306
azResult[14] = 16.4
Press any key to continue