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

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.cppCppSQLite3DB.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在窗体交互下简易实现几种数据库基本功能的简单说明!

 vc工程目录下 设置 Link  L/对象类模块  sqlite3.lib ,c/c++   分类  precompiled  header  选择不使用预补偿页眉

首先将SQLITE3的七个文件放在和vc工程文件同一目录下,在工程中加入CppSQLite3DB.cppCppSQLite3DB.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在窗体交互下简易实现几种数据库基本功能的简单说明!

 vc工程目录下 设置 Link  L/对象类模块  sqlite3.lib ,c/c++   分类  precompiled  header  选择不使用预补偿页眉

首先将SQLITE3的七个文件放在和vc工程文件同一目录下,在工程中加入CppSQLite3DB.cppCppSQLite3DB.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在窗体交互下简易实现几种数据库基本功能的简单说明!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值