今天要学习SQLITE3的类,codeproject上的这个作者写的C++类操作SQLIE3,很实用的说。膜拜一下。OMG
添加头文件 #include "CppSqlite3.h" 这个文件中,已经包含了#include "sqlite3.h"头文件。
定义数据库
CppSqlite3 db ;
获取数据库信息
db.GetVersion();//数据库版本信息
SQLiteHeaderVersion()
strMessage.Format("SQLiteHeaderVersion:%s SQLiteLibraryVersion:%s SQLiteLibraryVersionNumber:%d SQLiteVersion:%s", db.SQLiteHeaderVersion(),db.SQLiteLibraryVersion(),db.SQLiteLibraryVersionNumber(),db.SQLiteVersion());输出结果:SQLiteHeaderVersion:3.6.21 SQLiteLibraryVersion:3.6.22 SQLiteLibraryVersionNumber:3006022 SQLiteVersion:3.6.21
执行SQL语言
db.execDML("Create table");
插入SQL记录
int rows = db.execDML("insert into table ");
返回值为插入的记录条数;
int id = db.lastRowId();
获取最后一次的插入记录ID,适用于自动增长的主键。
删除SQL记录
int irows = db.execDML("delete * from table ");
返回值是删除的记录条数;
更新SQL记录
int rows = db.execDML("update table set rowid = 1");
返回值是更新的记录条数
使用事务控制
db.execDML("begin Transaction;");
db.execDML("insert into table .......");
db.execDML("commit Transaction;");
返回计算值
int iCount = db.execScalar("select count(*) from table;");
查询结果集
CppSqlite3Query q = db.execQuery("select * From table "); //查询
取结果集的字段信息
int iFiles = q.numFields() ;//总字段数
q.fieldName(1);//字段名称
q.fieldType(1);//字段类型
遍历结果集中的记录
while( !q.eof())
{
int id = q.fieldValue(0) ;//取字段值
q.nextRow();
}
格式化查询
可以格式条件以及插入NULL值
CppSqlite3Buff buffSQL;
buffSQL.format("insert into table(id,value) values(%Q,%Q)",NULL,"what's is it ?");
db.execDML(buffSQL);
按表查询
CppSqlite3Table t = db.getTable("select * From table ");
取表字段信息:
t.numFields();//总字段数
t.fieldName(0);//字段名称
t.fieldType(0);//字段类型
遍历表格结果
for(int row = 0 ;i<t.numRows();row ++)
{
t.setRow(row);//定位表格中的行
//取行信息
int (int i=0;i<t.numFields();i++){
if( !t.fieldIsNull(i))//判断空值
int id = t.fieldValue(i);//取字段值
}
}