wince下如何操作sqlite数据库

来源:互联网 发布:ug编程待遇怎么样工资 编辑:程序博客网 时间:2024/05/13 23:24

SQLite简介,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。 至今已经有12个年头,SQLite也迎来了一个版本 SQLite 3已经发布。

现在官网上下载sqlite3,http://www.sqlite.org/download.html

sqlite-amalgamation-3071401.zip
(1.32 MiB)This ZIP archive contains all C source code for SQLite 3.7.14.1 combined into a single source file (theamalgamation).
(sha1: dc64b561fd0f27f5cf2aece439aae12fc3b387cc)

选择这个包含4个文件取其中sqlite3.h.sqlite3.c两个文件。

现在新建工程选择mfc应用程序(方便快捷),然后添加上面2个文件,上面sqlite3.c不要预编译,如果用预编译会一连串错误。添加进来编译下通过。然后为了操作更加简洁可以在封装一下sqlite3.c里面的接口,写一个类MySQLite;

/**********************************************MySQLite.h********************************************/

#include <stdio.h> 

#include <stdlib.h> 

#include <string.h> 

#include "sqlite3.h"  

class MySQLite 


public:
 MySQLite(void);
  ~MySQLite(void);
private: 

 sqlite3 *db;//数据库句柄  
 char **azResult; //二维数组存放结果  
 char *zErrMsg;//保存错误信息  
 int row; 
 int column; 

public: 

 bool sqlite_connect(char *filename);//连接数据库  
 bool sqlite_exec(char *sql);//执行SQL命令  
 void sqlite_search(char *search_sql);//查询  
 bool sqlite_disconnect();//断开数据库连接  

 int GetTableRow() { return row; }//查询后,取得表“列”数  
 int GetTableColumn() { return column; }//查询后,取得表“栏”数  
 char *GetErrorMsg() { return zErrMsg; }//取得当前错误提示  
 char *GetTableData(int x,int y) { return *(azResult+x+y*column); }//查询后,取得表内某个单元值  

}; 

/***********************************************MySQLite.cpp*******************************************/

#include "StdAfx.h"
#include "MySQLite.h"

MySQLite::MySQLite(void)
{
}

MySQLite::~MySQLite(void)
{
}

bool MySQLite::sqlite_connect(char *filename) 

    db=NULL; 

    zErrMsg = 0; 

    row = 0, column = 0; 

    int rc; 

    rc = sqlite3_open(filename, &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件  

    if( rc ) 

    { 

       strcpy(zErrMsg,sqlite3_errmsg(db));//保存错误信息  

       sqlite3_close(db); 

       return false; 

    } 

    return true;    

 

bool MySQLite::sqlite_exec(char *sql) 

   int rc; 

   rc=sqlite3_exec( db , sql , 0 , 0 , &zErrMsg ); 

   if(rc == SQLITE_OK) 

       return true; 

   return false; 

void MySQLite::sqlite_search(char *search_sql) 

   sqlite3_get_table( db,search_sql,&azResult,&row,&column,&zErrMsg); 

 

bool MySQLite::sqlite_disconnect() 

   //释放掉 azResult 的内存空间  

   sqlite3_free_table( azResult ); 

   if(sqlite3_close(db)==SQLITE_OK) //关闭数据库  

    return true; 

   return false; 


/**************************************************end***************************************************/

然后在我们调用的地方初始化打开

 bool result = false; 

 MySQLite *sqlite=new MySQLite(); 

 result=sqlite->sqlite_connect("\\sdmmc\\test.db");      //打开数据库

 if(!result) cout<<sqlite->GetErrorMsg()<<endl; 

 

 result=sqlite->sqlite_exec("CREATE TABLE SensorData(ID INTEGER PRIMARY KEY,SensorID INTEGER,SiteNum INTEGER,Time VARCHAR(500),SensorParameterREAL);");               //创建表

 if(!result) cout<<sqlite->GetErrorMsg()<<endl; 

//下面插入两条数据
 result=sqlite->sqlite_exec("INSERT INTO SensorData(ID,SensorID,SiteNum,Time,SensorParameterREAL) VALUES(NULL , 1 , 1 , '200605011206', 18.9 );"); 

 if(!result) cout<<sqlite->GetErrorMsg()<<endl; 

 result=sqlite->sqlite_exec("INSERT INTO SensorData(ID,SensorID,SiteNum,Time,SensorParameterREAL) VALUES(NULL , 2 , 20 , '201209011206', 22.5 );"); 

 if(!result) cout<<sqlite->GetErrorMsg()<<endl; 

//查询数据

 sqlite->sqlite_search("SELECT * from SensorData"); 

 cout<<sqlite->GetTableColumn()<<"   "<<sqlite->GetTableRow()<<endl;  //打印行数和列数

//注意第一行是字段,我们其实sqlite->GetTableRow()是多少条数据。没包含字段。

 for (int i=0; i<=sqlite->GetTableRow();i++)
 {
  
  for (int j=0; j<sqlite->GetTableColumn();j++)
  {
   printf("%s, ",sqlite->GetTableData(j,i));
  }
  printf("\r\n");
 }

打印出结果

ID, SensorID, SiteNum, Time, SensorParameterREAL,

1, 1, 1, 200605011206, 18.9,

2, 2, 20, 201209011206, 22.5,