SQLite 下载与SQLite.lib生成
一、SQLite下载
SQLite可以在SQLite 官网http://www.sqlite.org/download.html下载 sqlite-dll-win32-x86-3070600.zip(这个时候发现压缩包里只有dll和def文件。)
二、SQLite.lib生成
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所在的当前路径,如:cd C:/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
生成.lib后,建立工程时要删除,要不然编译报错
2.
我生成的
sqlite3.lib
sqlite3.exp
下载地址:
http://download.csdn.net/source/3545615
3.
#include <Stdio.h>
//包含SQLite3头文件
#include "sqlite3.h"
//连接sqlite3库文件
#pragma comment(lib,"sqlite3.lib")
int main( int argc, char* argv[] )
{
int nRet;
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 , 0 , 0 , &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 , 1 , '200605011206', 18.9 );" ;
nRet = sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
if ( 0 != nRet )
{
printf("Insert Error:%s", zErrMsg);
}else
{
printf("Insert OK\n");
}
//insert data
sql = "INSERT INTO \"SensorData\" VALUES(NULL , 1 , 1 , '200605011306', 16.4 );" ;
nRet = sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
if ( 0 != nRet )
{
printf("Insert Error:%s", 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 , sql , 0 , 0 , &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 , sql , 0 , 0 , &zErrMsg );
if ( 0 != nRet )
{
printf("Delete Error:%s", zErrMsg);
}else
{
printf("Delete OK\n");
}
//query data
int nrow = 0, ncolumn = 0;
char **azResult; //二维数组存放結果
sql = "SELECT * FROM SensorData ";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
int i = 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 );
return 0;
}
注:请将您的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,SensorID INTEGER,SiteNum INTEGER,Time VARCHAR(12),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