一、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. mysql 获取任意一个月的所有天数。

    SELECT ADDDATE(y.first, x.d - 1) as dFROM(SELECT 1 AS d UNION ALLSELECT 2 UNION ALLSELECT 3 UNION AL ...

  2. 集训队8月1日(拓扑排序+DFS+主席树入门)

    上午看书总结 今天上午我看了拓扑排序,DFS+剪枝,相当于回顾了一下,写了三个比较好的例题.算法竞赛指南93~109页. 1.状态压缩+拓扑排序 https://www.cnblogs.com/246 ...

  3. iOS 获取self类型

    类型转换快速写法 typeof(self) bself = self; 版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. Linux下安装Tomcat服务器

    Linux下安装Tomcat服务器 一.总结 一句话总结: linux多用才能熟 1.阿里云上面我们买的服务器,怎么让它可以访问特定的端口? 就是给服务器的安全组添加规则:实例-->更多--&g ...

  5. JS-插件编写

    # 参数处理 JS: function plugin_mian_func(options){ var defaluts = { opt1: 'opt1', opt2: 'opt2', opt3: { ...

  6. 2019浙江省赛 Strings in the Pocket【manacher】

    Strings in the Pocket 题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意 给你两个字符 ...

  7. Django框架(十四)—— Django分页组件

    目录 Django分页组件 一.分页器 二.分页器的使用 三.案例 1.模板层 2.视图层 Django分页组件 一.分页器 数据量大的话,可以分页获取,查看 例如:图书管理中,如果有成千上万本书,要 ...

  8. 元类,sqlalchemy查询

    import sqlalchemy from sqlalchemy.ext.declarative import declarative_base #创建连接实例 db = sqlalchemy.cr ...

  9. mysqldump备份和恢复

    一.备份单个数据库 1.备份命令:mysqldump MySQL数据库自带的一个很好用的备份命令.是逻辑备份,导出 的是SQL语句.也就是把数据从MySQL库中以逻辑的SQL语句的形式直接输出或生成备 ...

  10. JPA接口整理归纳方法规则

    Keyword Sample JPQL snippet And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ...