您的位置:首页 > 编程语言 > C语言/C++

Cppslqite的使用

2016-04-07 14:11 369 查看

Cppslqite的使用

环境

Windows7
sqlite3.lib
sqlite3.dll
sqlite3.h
CppSqlite3.h
CppSqlite3.cpp


VS工程配置



增删改

bind只能用于增删改?

int CDataAccessLayer::Data_SaveToDB(TickValue* ptr, int  dataLen)
{
try
{
database.open("E:\\xiangheng\\Documents\\JX_VS2013_Projects\\MoveDataToSqlite\\MoveDataToSqlite\\TestSqlite3.db");
database.execDML("begin transaction");
CppSQLite3Statement smt =
database.compileStatement("insert into Ticks(instrumentCode,exchangeCode,tradeTime,open,close,high,low,volume,interest,buyPrice,buyVolume,sellPrice,sellVolume) values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
for (int i = 0; i < dataLen; i++)
{
smt.bind(1, ptr[i].instrumentCode);//str
smt.bind(2, ptr[i].exchangeCode);//str
//TODO:tradeTime这个参数为0的话,需要新建一张表
//但是如果每次都检查一遍的话,效率会比较低
smt.bind(3, ptr[i].tradeTime);
if (0.0f == ptr[i].tradeTime)
{
//TODO:如果不存在某张表,就新建一张Tick数据表

}
smt.bind(4, ptr[i].open);
smt.bind(5, ptr[i].close);
smt.bind(6, ptr[i].high);
smt.bind(7, ptr[i].low);
smt.bind(8, ptr[i].volume);
smt.bind(9, ptr[i].interest);
smt.bind(10, ptr[i].buyPrice);
smt.bind(11, ptr[i].buyVolume);
smt.bind(12, ptr[i].sellPrice);
smt.bind(13, ptr[i].sellVolume);
smt.execDML();
}

database.execDML("commit transaction");
smt.finalize();
database.close();
}
catch (CppSQLite3Exception* e)
{
string msg = e->errorMessage();
return -1;
}

return 1;
}


查询

#define dbpath "E:\\xiangheng\\Documents\\JX_VS2013_Projects\\MoveDataToSqlite\\MoveDataToSqlite\\TestSqlite3.db"

/*
这个方法差不多做完了
*/
int CDataAccessLayer::Data_GetHqDataFromDB(string strCode, int dlt, double startTime, double endTime, TickValue* ptr, int & dataLen)
{

//方法有可能只读取一张表,也可能读取好几张表
//暂时是只考虑读取一张表的情况
//读取多张表的情况后面再考虑
//现在先把关于多张表的思考记下来
//1.定义容器vector<string> vecTable,容器中存这几张表的名字
//2.定义容器vector<string> vecSql,容器中存放这几个表对应的sql语句
//问题来了:db中的数据是否完整?

//根据dlt来判断读取哪种表
//根据strCode来选择哪张表
string strTable = "Tick_" + strCode;
int rst = TickType::type(dlt);
if (1 == rst)
{
strTable += "_0";
}

database.open(dbpath);

//startTime,endTime是查询条件之一
char sqlQuery[512];
sprintf(sqlQuery, "select * from %s where tradeTime>%01f AND tradeTime<%01f", strTable.c_str(), startTime, endTime);
CppSQLite3Query query = database.execQuery(sqlQuery);
int nCnt = 0;
vector<TickValue> vec;

while (!query.eof())
{
TickValue tt;
memset(&tt, 0, sizeof(TickValue));

strcpy(tt.instrumentCode, query.getStringField("instrumentCode"));
strcpy(tt.exchangeCode, query.getStringField("exchangeCode"));
tt.tradeTime = query.getIntField("tradeTime");
tt.open = query.getFloatField("open");
tt.close = query.getFloatField("close");
tt.high = query.getFloatField("high");
tt.low = query.getFloatField("low");
tt.volume = query.getFloatField("volume");
tt.interest = query.getFloatField("interest");
tt.buyPrice = query.getFloatField("buyPrice");
tt.buyVolume = query.getFloatField("buyVolume");
tt.sellPrice = query.getFloatField("sellPrice");
tt.sellVolume = query.getFloatField("sellVolume");

vec.push_back(tt);//直接拷贝对象到容器
nCnt++;
query.nextRow();
}

if (dataLen >= nCnt)//避免写缓冲区溢出
{
//将容器中的数据拷贝到TickValue的写缓冲区中
nCnt = 0;
for (auto t : vec)
{
memcpy(&ptr[nCnt++], &t,sizeof(t));
}
}
dataLen = nCnt;
query.finalize();
database.close();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: