SQLITE3 在VC/MFC 中使用的一点体会

类别:VC语言 点击:0 评论:0 推荐:

SQLITE简介:

This is an extension for the SQLite Embeddable SQL Database Engine. SQLite is a C library that implements an embeddable SQL database engine. Programs that link with the SQLite library can have SQL database access without running a separate RDBMS process.

SQLite is not a client library used to connect to a big database server. SQLite is the server. The SQLite library reads and writes directly to and from the database files on disk.

在 vc工程目录下 设置 Link  L/对象类模块 为 sqlite3.lib

                     c/c++   分类  precompiled  header  选择不使用预补偿页眉

 

首先将SQLITE3的七个文件放在和vc工程文件同一目录下

在工程中加入 CppSQLite3DB.cpp和CppSQLite3DB.h文件

 

# include "CppSQLite3.h"

extern CppSQLite3DB db;/////数据库对象

 

    remove("c:\\test.db");

    db.open("c:\\test.db");///打开数据库文件

   

建立表格:

db.execDML("create table Customer(CustomerName char(50), RoomNumber int, CustomerId int, ComeTime int,Money int);");

  db.execDML("create table Room(RoomNumber int, RoomPrice int, RoomState char[20]);");

  db.execDML("create table Manager(ManagerName char[20],PassWord int);");

插入数据:

 string szCmd;

 string szName = m_data1;

szCmd = "insert into Customer values(" ;

 szCmd +="'";

 szCmd +=szName;

szCmd +="'";

szCmd +=",";

sprintf(a,"%d",m_data2);

szCmd += a;

szCmd += ",";

sprintf(b,"%d",m_data3);

szCmd +=b;

szCmd +=",";

sprintf(c,"%d",m_data4);

szCmd += c;

szCmd += ",";

sprintf(d,"%d",m_data5);

szCmd += d;

szCmd += ");";

db.execDML(szCmd.c_str());/////////////插入SQL语句

MessageBox("提交成功,请继续!");

 

更新数据:

   char k[20];//提交后更新房间信息

                   string  szCkd;

                   szCkd="update Room  set  RoomState = 'notnull' where  RoomNumber=";

                      sprintf(k,"%d",m_data2);

                      szCkd +=k;

                      szCkd +=";";

              db.execDML(szCkd.c_str());  //////////插入SQL语句

 

查询数据:

CppSQLite3Query q = db.execQuery("select * from Customer;");

         while(!q.eof()){

          UpdateData(true);

                if(m_data1==q.getIntField(1))

                {

               

 

s.m_data1=q.getStringField(0);

                s.m_data2=q.getIntField(1);

                s.m_data3=q.getIntField(2);

                s.m_data4=q.getFloatField(3);

                s.m_data5=q.getIntField(4);

          s.DoModal();

                UpdateData(false);

                break;}

                q.nextRow();

                if(q.eof()==true) 

                       MessageBox("本旅馆无该房间!");

         }

 

 

删除数据:

char a[20];

  sprintf(a,"%d",m_data2);

  string szCmd;

  szCmd="delete from Room where RoomNumber =" ;

  szCmd+=a;

  szCmd+=";";

  int b=0;

  b=db.execDML(szCmd.c_str());

  if(b==0) 

  MessageBox("操作错误,没有该房间信息");

  else

  MessageBox("删除房间信息成功");

 

以上为SQLITE3在窗体交互下简易实现几种数据库基本功能的简单说明!

本文地址:http://com.8s8s.com/it/it515.htm