VC2005使用SQLite,适用于WIN32以及WINCE

首先,把编译SQLITE生成的DLL、LIB和sqlite3.h 放到项目的文件夹下,把项目=》属性=》链接器=》输入=》附加依赖项:输入SQLITE的lib文件名

  1. 一、创建MySQLite.cpp:  
  2.  
  3. #include "stdafx.h"  
  4.   
  5. #include "MySQLite.h"  
  6.   
  7. bool MySQLite::sqlite_connect(char *filename)  
  8.   
  9. {  
  10.   
  11.     db=NULL;  
  12.   
  13.     zErrMsg = 0;  
  14.   
  15.     row = 0, column = 0;  
  16.   
  17.     int rc;  
  18.   
  19.     rc = sqlite3_open(filename, &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件  
  20.   
  21.     if( rc )  
  22.   
  23.     {  
  24.   
  25.         strcpy(zErrMsg,sqlite3_errmsg(db));//保存错误信息  
  26.   
  27.         sqlite3_close(db);  
  28.   
  29.         return false;  
  30.   
  31.     }  
  32.   
  33.     return true;     
  34.   
  35. }  
  36.   
  37.   
  38.   
  39. bool MySQLite::sqlite_exec(char *sql)  
  40.   
  41. {  
  42.   
  43.     int rc;  
  44.   
  45.     rc=sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );  
  46.   
  47.     if(rc == SQLITE_OK)  
  48.   
  49.         return true;  
  50.   
  51.     return false;  
  52.   
  53. }  
  54.   
  55.   
  56.   
  57. void MySQLite::sqlite_search(char *search_sql)  
  58.   
  59. {  
  60.   
  61.     sqlite3_get_table( db,search_sql,&azResult,&row,&column,&zErrMsg);  
  62.   
  63. }  
  64.   
  65.   
  66.   
  67. bool MySQLite::sqlite_disconnect()  
  68.   
  69. {  
  70.   
  71. //释放掉 azResult 的内存空间  
  72.   
  73.     sqlite3_free_table( azResult );  
  74.   
  75.     if(sqlite3_close(db)==SQLITE_OK) //关闭数据库  
  76.   
  77.         return true;  
  78.   
  79.     return false;  
  80.   
  81. }  
  82.   
  83.   
  84.   
  85. 二、创建MySQLite.h:  
  86.  
  87. #include <stdio.h>  
  88.   
  89. #include <stdlib.h>  
  90.  
  91. #include <string.h>  
  92.   
  93. #include "sqlite3.h"  
  94.   
  95. class MySQLite  
  96.   
  97. {  
  98.   
  99. private:  
  100.   
  101.     sqlite3 *db;//数据库句柄  
  102.   
  103.     char **azResult; //二维数组存放结果  
  104.   
  105.     char *zErrMsg;//保存错误信息  
  106.   
  107.     int row;  
  108.   
  109.     int column;  
  110.   
  111. public:  
  112.   
  113.     bool sqlite_connect(char *filename);//连接数据库  
  114.   
  115.     bool sqlite_exec(char *sql);//执行SQL命令  
  116.   
  117.     void sqlite_search(char *search_sql);//查询  
  118.   
  119.     bool sqlite_disconnect();//断开数据库连接  
  120.   
  121.   
  122.   
  123.     int GetTableRow(){return row;}//查询后,取得表“列”数  
  124.   
  125.     int GetTableColumn(){return column;}//查询后,取得表“栏”数  
  126.   
  127.     char *GetErrorMsg(){return zErrMsg;}//取得当前错误提示  
  128.   
  129.     char *GetTableData(int x,int y){return *(azResult+x+y*column);}//查询后,取得表内某个单元值  
  130.   
  131. };  
  132.   
  133.   
  134.   
  135. 三、创建测试文件test.cpp:  
  136.  
  137.  
  138.  
  139. #include "stdafx.h"  
  140.   
  141. #include <iostream>  
  142.  
  143. #include "MySQLite.h"  
  144.   
  145. #define _DEBUG_  
  146.   
  147. using namespace std;  
  148.   
  149. int main( void )  
  150.   
  151. {  
  152.   
  153.     bool result;  
  154.   
  155.     MySQLite *sqlite=new MySQLite();  
  156.   
  157.     result=sqlite->sqlite_connect("hellogv.db");  
  158.   
  159.     if(!result) cout<<sqlite->GetErrorMsg()<<endl;  
  160.   
  161.      
  162.   
  163.     result=sqlite->sqlite_exec("CREATE TABLE SensorData(ID INTEGER PRIMARY KEY,SensorID INTEGER,SiteNum INTEGER,Time VARCHAR(500),SensorParameterREAL);");  
  164.   
  165.     if(!result) cout<<sqlite->GetErrorMsg()<<endl;  
  166.   
  167.   
  168.   
  169.     result=sqlite->sqlite_exec("INSERT INTO \"SensorData\" VALUES(NULL , 1 , 1 , '200605011206', 18.9 );");  
  170.   
  171.     if(!result) cout<<sqlite->GetErrorMsg()<<endl;  
  172.   
  173.   
  174.   
  175.   
  176.   
  177.     result=sqlite->sqlite_exec("INSERT INTO \"SensorData\" VALUES(NULL , 23 , 45 , '200605011306', 16.4 );");  
  178.   
  179.     if(!result) cout<<sqlite->GetErrorMsg()<<endl;  
  180.   
  181.   
  182.   
  183.     sqlite->sqlite_search("SELECT * from SensorData");  
  184.   
  185.     cout<<sqlite->GetTableColumn()<<"   "<<sqlite->GetTableRow()<<endl;  
  186.   
  187.     cout<<sqlite->GetTableData(0,0)<<endl;  
  188.   
  189.   
  190.   
  191.     sqlite->sqlite_disconnect();  
  192.   
  193.   
  194.   
  195.     return 0;  
  196.   
  197. }  
posted on 2010-06-30 12:15  二地主  阅读(599)  评论(0编辑  收藏  举报