qt中使用sqlite存储数据
一、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
qt中使用sqlite存储数据的更多相关文章
- Android开发手记(18) 数据存储三 SQLite存储数据
Android为数据存储提供了五种方式: 1.SharedPreferences 2.文件存储 3.SQLite数据库 4.ContentProvider 5.网络存储 SQLite 是以嵌入式为目的 ...
- [ Android 五种数据存储方式之三 ] —— SQLite存储数据
SQLite是轻量级嵌入式数据库引擎,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, Python)都使用了 ...
- 在Qt中使用SQLite数据库
前言 SQLite(sql)是一款开源轻量级的数据库软件,不需要server,可以集成在其他软件中,非常适合嵌入式系统. Qt5以上版本可以直接使用SQLite(Qt自带驱动). 用法 1 准备 引入 ...
- android中使用SharedPreferences存储数据
使用SharedPreferences存储数据还是比较简单的 1.添加或修改数据(没有数据就添加,有数据就是修改): SharedPreferences.Editor editor = getShar ...
- Qt中提高sqlite的读写速度(使用事务一次性写入100万条数据)
SQLite数据库本质上来讲就是一个磁盘上的文件,所以一切的数据库操作其实都会转化为对文件的操作,而频繁的文件操作将会是一个很好时的过程,会极大地影响数据库存取的速度.例如:向数据库中插入100万条数 ...
- 在Activity和Application中使用SharedPreferences存储数据
1.在Activity中创建SharedPreferences对象及操作方法 SharedPreferences pre=getSharedPreferences("User", ...
- 安卓入门——————简单记账本的开发(用sqlite存储数据)(一)
设计思想————首先要确定有几个页面.和每个页面的大致布局 由于是入门,我也是学习了不是很长的时间,所以项目比较low.... 第一个页面,也就是打开APP的首页面: 今天这个博客,先实现添加功能!: ...
- cocos2d-x中几种存储数据的方式
说明:本文所论述内容均基于cocos2dx 3.0 版本. 1.UserDefault 它是cocos2d-x用来存取基本数据类型用的.保存为XML文件格式. 查看CCUserDefault文件,可以 ...
- 使用SQLite存储数据
一.SQLiteAndroid 为了让我们能够更加方便地管理数据库, 专门提供了一个SQLiteOpenHelper 帮助类,借助这个类就可以非常简单地对数据库进行创建和升级. 1.SQLiteOpe ...
随机推荐
- npm启动报错
npm 启动报错 event.js 160 报错原因: 端口号被占用 解决方法: 1.重新定义一个端口号. 2.任务管理器关掉node进程,重新运行npm.
- 【杂】聊聊我的男神:Jordan Peterson
这篇文章我打算聊聊我的男神Jordan Peterson(简称JP).如果还不太了解JP,那么下面两个链接是JP的背景介绍: [文字]Jordan Peterson From Wikipedia, t ...
- iview下拉树组件
iview.vue.jq等自行引用 iview.js和iview.css版本是iview@3.4.2 <!DOCTYPE html> <html lang="en" ...
- JMS Activemq实战例子demo
上一篇已经讲了JMS的基本概念,这一篇来上手练一练,如果对JMS基本概念还不熟悉,欢迎参靠JMS基本概. 这篇文章所使用的代码已经不是我刚入手时的代码,已经经过我重构过的代码,便于理解,并且加了很多中 ...
- boot、cloud
最近在学习Spring Boot也整理了一些文章,有需要的可以参考一下 https://www.zhihu.com/question/39483566 Spring Cloud是一系列框架的有序集合. ...
- [六省联考2017]分手是祝愿 题解(期望dp)
题目描述 B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态,下标为从 1 到 n 的正整数. 每个灯有两个状态亮和灭,我们用 1 来表示这个灯是亮的,用 0 表示 ...
- 漫谈C语言结构体【转】
相信大家对于结构体都不陌生.在此,分享出本人对C语言结构体的学习心得.如果你发现这个总结中有你以前所未掌握的,那本文也算是有点价值了.当然,水平有限,若发现不足之处恳请指出.代码文件test.c我放在 ...
- table td 溢出隐藏
需要给table加一个属性:table-layout:fixed;
- springboot核心原理
1.基于你对springboot的理解描述一下什么是springboot 它是一个服务于spring框架的框架,能够简化配置文件,快速构建web应用, 内置tomcat,无需打包部署,直接运行. 2. ...
- MUI对话框使用
一.alert告警框 用法 .alert(message,title,btnvalue,callback[,type]); document.getElementById("noclick& ...