一、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存储数据的更多相关文章

  1. Android开发手记(18) 数据存储三 SQLite存储数据

    Android为数据存储提供了五种方式: 1.SharedPreferences 2.文件存储 3.SQLite数据库 4.ContentProvider 5.网络存储 SQLite 是以嵌入式为目的 ...

  2. [ Android 五种数据存储方式之三 ] —— SQLite存储数据

    SQLite是轻量级嵌入式数据库引擎,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, Python)都使用了 ...

  3. 在Qt中使用SQLite数据库

    前言 SQLite(sql)是一款开源轻量级的数据库软件,不需要server,可以集成在其他软件中,非常适合嵌入式系统. Qt5以上版本可以直接使用SQLite(Qt自带驱动). 用法 1 准备 引入 ...

  4. android中使用SharedPreferences存储数据

    使用SharedPreferences存储数据还是比较简单的 1.添加或修改数据(没有数据就添加,有数据就是修改): SharedPreferences.Editor editor = getShar ...

  5. Qt中提高sqlite的读写速度(使用事务一次性写入100万条数据)

    SQLite数据库本质上来讲就是一个磁盘上的文件,所以一切的数据库操作其实都会转化为对文件的操作,而频繁的文件操作将会是一个很好时的过程,会极大地影响数据库存取的速度.例如:向数据库中插入100万条数 ...

  6. 在Activity和Application中使用SharedPreferences存储数据

    1.在Activity中创建SharedPreferences对象及操作方法 SharedPreferences pre=getSharedPreferences("User", ...

  7. 安卓入门——————简单记账本的开发(用sqlite存储数据)(一)

    设计思想————首先要确定有几个页面.和每个页面的大致布局 由于是入门,我也是学习了不是很长的时间,所以项目比较low.... 第一个页面,也就是打开APP的首页面: 今天这个博客,先实现添加功能!: ...

  8. cocos2d-x中几种存储数据的方式

    说明:本文所论述内容均基于cocos2dx 3.0 版本. 1.UserDefault 它是cocos2d-x用来存取基本数据类型用的.保存为XML文件格式. 查看CCUserDefault文件,可以 ...

  9. 使用SQLite存储数据

    一.SQLiteAndroid 为了让我们能够更加方便地管理数据库, 专门提供了一个SQLiteOpenHelper 帮助类,借助这个类就可以非常简单地对数据库进行创建和升级. 1.SQLiteOpe ...

随机推荐

  1. U200-SNMP

    U200配置snmp snmp-agent sys-info version v2c snmp-agent community read jkzh snmp-agent trap enable snm ...

  2. PHP filter_list() 函数

    定义和用法 filter_list() 函数返回包含所有得到支持的过滤器的一个数组. 语法 filter_list() 提示和注释 注释:该函数的结果不是过滤器 ID,而是过滤器名称.请使用 filt ...

  3. layui导出表格全部数据

    layui自带的导出表格,只能导出当前页面,如果当前页包含全部数据,那不就是导出全部数据了吗,所以我给导出事件单独定义了一个请求,当触发这个请求时,在后台查询数据时不要按接收的page 和 limit ...

  4. [NOIP模拟26]题解

    今天的考试题改自闭了……所以滚来写陈年题解. A.*****贪婪***** RT,出题人告诉我们这题要贪心. 最优的策略一定是拖到必须断的时候再断开(虽然并不知道为什么). 如果一段序列满足题目中的性 ...

  5. c 语言 volatile 关键字

    一.前言 1.编译器优化介绍: 由于内存访问速度远不及CPU处理速度,为提高机器整体性能,在硬件上引入硬件高速缓存Cache,加速对内存的访问.另外在现代CPU中指令的执行并不一定严格按照顺序执行,没 ...

  6. 阿里云epel源

    epel是个好东西,不过国外的速度实在是不能忍受.所以 有了这篇文章.1. 首先卸载以前装的epel以免影响 rpm -e epel-release 2. 下载阿里提供的epel wget -P /e ...

  7. Nginx网络架构实战学习笔记(六):服务器集群搭建、集群性能测试

    文章目录 服务器集群搭建 Nginx---->php-fpm之间的优化 302机器 202机器 压力测试 搭建memcached.mysql(数据准备) 今晚就动手-.- 集群性能测试 服务器集 ...

  8. Rsync 实现服务器文件的同步——服务端的安装配置

    一.安装rsync 直接使用yum命令进行安装即可. yum -y install rsync 二.配置文件 网上大多教程都说安装是默认没有配置文件的,但是经过我的尝试,yum安装下默认是有配置文件的 ...

  9. 第一周复习二 (CSS样式表及其属性)

    样式表三种写法 1内联写法:style直接写在标签内.个人感觉多用于个别标签,一般情况优先级较高 style="font-size:16px;" 2内嵌写法:写在<head& ...

  10. 使用 C++ 编写的基础 Windows 服务 (CppWindowsService)

    最近项目中涉及到使用C++写一个后台服务程序,找了很多资料,还是使用Google搜索找到了比较详细点的资料,就是从微软官方MSDN的例子,如下: 使用 C++ 编写的基础 Windows 服务 (Cp ...