qt中使用sqlite存储数据

时间:2022-04-13 20:09:27

一、sqilte的安装

在Windows上安装SQLite:

请访问 SQLite 下载页面,从 Windows 区下载预编译的二进制文件。

您需要下载 sqlite-tools-win32-*.zip 和 sqlite-dll-win32-*.zip 压缩文件。

创建文件夹 C:\sqlite,并在此文件夹下解压上面两个压缩文件,将得到 sqlite3.def、sqlite3.dll 和 sqlite3.exe 文件。

添加 C:\sqlite 到 PATH 环境变量,最后在命令提示符下,使用 sqlite3 命令,将显示如下结果。

C:\>sqlite3
SQLite version 3.7.15.2 -- ::
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

二、sqilte的连接

 /* 名称:ConnectMySqlite
* 功能:确认数据库连接
* 输入:_database需要连接的数据库
* 返回:true连接成功,false连接失败
*/
bool MySql::ConnectMySqlite(QSqlDatabase &_database)
{
if(QSqlDatabase::contains(mySqlConnectName))
_database = QSqlDatabase::database(mySqlConnectName);
else
_database = QSqlDatabase::addDatabase("QSQLITE", mySqlConnectName);
_database.setDatabaseName(mySqlName); if(!_database.open()) {
qDebug()<<"ConnectMySqlite:SQLite connected failed.";
return false;
} return true;
}

ConnectMySqlite

三、qt中QSqlQuery使用

 /* 名称:UpdateAlertTableValue
* 功能:更新myAlertTable结构
* 输入:无
* 返回:0成功,-1数据库连接失败-2数据更新不全
*/
int MySql::UpdateAlertTableValue()
{
QString sqlSelect = QString("select * from %1").arg(mySqlAlertTableName);
int i=;
bool status = false;
QSqlDatabase myDataBase;
status = ConnectMySqlite(myDataBase);
if(!status)
{
qDebug()<<"UpdateAlertTableValue>>ConnectMySqlite failed.";
return -;
} if(!myDataBase.open())
return -; QSqlQuery query(myDataBase);
query.prepare(sqlSelect);
if(!query.exec()) {
qDebug()<<"exec error:"<<query.lastError();
} else {
for(i =;i< && query.next();++i)
{
myAlertTable[i].id = query.value().toInt();
myAlertTable[i].alertName = query.value().toString();
myAlertTable[i].alertValue= query.value().toInt();
myAlertTable[i].alertInterval = query.value().toInt();
myAlertTable[i].relayStatus0 = query.value().toInt();
myAlertTable[i].relayStatus1 = query.value().toInt();
myAlertTable[i].relayStatus2 = query.value().toInt();
myAlertTable[i].relayStatus3 = query.value().toInt();
myAlertTable[i].diffQuitiety = query.value().toInt();
}
} if(i!=)
return -;
return ;
}

UpdateAlertTableValue

四、qt中QSqlQueryModel使用

 /* 名称:ExportSqliteDataWithModel
* 功能:导出为excel
* 输入:无
* 返回:无
*/
void MySql::ExportSqliteDataWithModel(QDateTime _startDate, QDateTime _endDate, QString filePath)
{
QString path = filePath;
QString sqlSelect = QString("select * from %1 where %1.sampleDate between '%2' and '%3' ")
.arg(mySqlStormTableName)
.arg(_startDate.toString("yyyy-MM-dd hh:mm:ss"))
.arg(_endDate.toString("yyyy-MM-dd hh:mm:ss")); bool status = false;
QSqlDatabase myDataBase;
status = ConnectMySqlite(myDataBase);
if(!status)
{
qDebug()<<"ExportSqliteDataWithModel>>ConnectMySqlite failed.";
return;
} if(!myDataBase.open())
return; QSqlQueryModel *model = new QSqlQueryModel(this);
QFile file(path);
QTextStream out(&file); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug()<<file.errorString();
return;
} else {
uint excelMaxrows = ;
model->setQuery(sqlSelect, myDataBase); while(model->canFetchMore())
{
model->fetchMore();
}
qDebug()<<"path:"<<path<<"filename:"<<file.fileName(); uint tableRows = model->rowCount();
uint64_t row = ;
for(uint i=;row<tableRows;++i)
{
if(i!=) //如果数据超过1000000行,创建新文件
{
path = path.replace(".xls",QString("(%1).xls").arg(i));
file.setFileName(path);
out.setDevice(&file);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug()<<file.errorString();
return;
}
} for(;row<tableRows && row<excelMaxrows*i;++row)
{
QModelIndex indexId = model->index(row,);
QModelIndex indexHighValue = model->index(row,);
QModelIndex indexLowValue = model->index(row,);
QModelIndex indexTempValue = model->index(row,);
QModelIndex indexSampleData = model->index(row,); out<<indexId.data().toInt()<<"\t"<<indexHighValue.data().toDouble()
<<"\t"<<indexLowValue.data().toDouble()<<"\t"<<indexTempValue.data().toDouble()
<<"\t"<<indexSampleData.data().toString();
out<<"\n";
}
file.close();
}
}
}

ExportSqliteDataWithModel