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 ...
随机推荐
- C# 在Word表格中插入新行(表格含合并行)
public string CreateWordFile(string CheckedInfo) { string message = "" ...
- 【和孩子一起学编程】 python笔记--第四天
第十一章: 可变循环 newStars = int(input("how many stars do you want?")) for i in range(newStars): ...
- java html生成图片html2canvas,Canvas2Image
<html> <div id="pic" style="margin-left: 500px;position:fixed;opacity:0;&quo ...
- UNP学习 广播
一.概述 虽然UDP支持各种形式的地址,但TCP只支持单播地址. 上图要点是: IPv4对多播的支持是可选的,而IPv6则时必须的. IPv6没有提供对广播的支持:当使用广播的IPv4应用程序一直到I ...
- Tomcat集群搭建超详细(apache+mod_jk+tomcat)
TOMCAT集群 目录 TOMCAT集群 1 1 集群 1 1.1 什么是集群 1 1.2 集群的特性 1 1.3 集群的分类 1 1.4 TOMCAT集群配置的优缺点 2 1.5 APACHE+TO ...
- 96、搬家到csdn
大家好: 今天开始会将所有的博客搬家到CSDN,以后请参考CSDN上的博客:http://blog.csdn.net/u012416045 谢谢 维真
- 52、saleforce 导入csv文件
Load Data Using the Custom Object Import Wizard 1. 2. 3. 4. 5. 6.然后就导入成功了
- 大数据学习笔记之Linux基础(一):Linux初窥
文章目录 一.Linux入门概述 1.1 概述 1.2 下载地址 1.3 Linux特点 1.4 Linux和Windows区别 二.VM安装相关 2.1 安装VMWare虚拟机 2.2 安装Cent ...
- Mosaic
Mosaic 是最古老的WEB浏览器,这是个套件FOR WINDOWS(包括TCP/IP,拨号,EMAIL等).
- linux 杂七杂八
一."init"是内核启动的第一个用户空间程序(PID=1),也是所有用户态进程的"大总管":所有内核态进程的大总管是PID=2的[kthreadd]: 二.v ...