首页 / SQLITE / qt中使用sqlite存储数据
qt中使用sqlite存储数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了qt中使用sqlite存储数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4503字,纯文字阅读大概需要7分钟。
内容图文

一、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.22013-01-0911:53:05 Enter ".help"for instructions Enter SQL statements terminated with a ";" sqlite>
二、sqilte的连接


1 /* 名称:ConnectMySqlite 2 * 功能:确认数据库连接 3 * 输入:_database需要连接的数据库 4 * 返回:true连接成功,false连接失败 5 */ 6 bool MySql::ConnectMySqlite(QSqlDatabase &_database) 7{ 8if(QSqlDatabase::contains(mySqlConnectName)) 9 _database = QSqlDatabase::database(mySqlConnectName); 10else11 _database = QSqlDatabase::addDatabase("QSQLITE", mySqlConnectName); 12 _database.setDatabaseName(mySqlName); 1314if(!_database.open()) { 15 qDebug()<<"ConnectMySqlite:SQLite connected failed."; 16returnfalse; 17 } 1819returntrue; 20 }
三、qt中QSqlQuery使用


1 /* 名称:UpdateAlertTableValue 2 * 功能:更新myAlertTable结构 3 * 输入:无 4 * 返回:0成功,-1数据库连接失败-2数据更新不全 5 */ 6 int MySql::UpdateAlertTableValue() 7 { 8 QString sqlSelect = QString("select * from %1").arg(mySqlAlertTableName); 9int i=0; 10bool status = false; 11 QSqlDatabase myDataBase; 12 status = ConnectMySqlite(myDataBase); 13if(!status) 14 { 15 qDebug()<<"UpdateAlertTableValue>>ConnectMySqlite failed."; 16return -1; 17 } 1819if(!myDataBase.open()) 20return -1; 2122 QSqlQuery query(myDataBase); 23 query.prepare(sqlSelect); 24if(!query.exec()) { 25 qDebug()<<"exec error:"<<query.lastError(); 26 } else { 27for(i =0;i<6 && query.next();++i) 28 { 29 myAlertTable[i].id = query.value(0).toInt(); 30 myAlertTable[i].alertName = query.value(1).toString(); 31 myAlertTable[i].alertValue= query.value(2).toInt(); 32 myAlertTable[i].alertInterval = query.value(3).toInt(); 33 myAlertTable[i].relayStatus0 = query.value(4).toInt(); 34 myAlertTable[i].relayStatus1 = query.value(5).toInt(); 35 myAlertTable[i].relayStatus2 = query.value(6).toInt(); 36 myAlertTable[i].relayStatus3 = query.value(7).toInt(); 37 myAlertTable[i].diffQuitiety = query.value(8).toInt(); 38 } 39 } 4041if(i!=6) 42return -2; 43return0; 44 }
四、qt中QSqlQueryModel使用


1 /* 名称:ExportSqliteDataWithModel 2 * 功能:导出为excel 3 * 输入:无 4 * 返回:无 5 */ 6 void MySql::ExportSqliteDataWithModel(QDateTime _startDate, QDateTime _endDate, QString filePath) 7 { 8 QString path = filePath; 9 QString sqlSelect = QString("select * from %1 where %1.sampleDate between ‘%2‘ and ‘%3‘ ") 10 .arg(mySqlStormTableName) 11 .arg(_startDate.toString("yyyy-MM-dd hh:mm:ss")) 12 .arg(_endDate.toString("yyyy-MM-dd hh:mm:ss")); 1314bool status = false; 15 QSqlDatabase myDataBase; 16 status = ConnectMySqlite(myDataBase); 17if(!status) 18 { 19 qDebug()<<"ExportSqliteDataWithModel>>ConnectMySqlite failed."; 20return; 21 } 2223if(!myDataBase.open()) 24return; 2526 QSqlQueryModel *model = new QSqlQueryModel(this); 27 QFile file(path); 28 QTextStream out(&file); 2930if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { 31 qDebug()<<file.errorString(); 32return; 33 } else { 34uint excelMaxrows = 1000000; 35 model->setQuery(sqlSelect, myDataBase); 3637while(model->canFetchMore()) 38 { 39 model->fetchMore(); 40 } 41 qDebug()<<"path:"<<path<<"filename:"<<file.fileName(); 4243uint tableRows = model->rowCount(); 44 uint64_t row = 0; 45for(uint i=1;row<tableRows;++i) 46 { 47if(i!=1) //如果数据超过1000000行,创建新文件48 { 49 path = path.replace(".xls",QString("(%1).xls").arg(i)); 50 file.setFileName(path); 51out.setDevice(&file); 52if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { 53 qDebug()<<file.errorString(); 54return; 55 } 56 } 5758for(;row<tableRows && row<excelMaxrows*i;++row) 59 { 60 QModelIndex indexId = model->index(row,0); 61 QModelIndex indexHighValue = model->index(row,1); 62 QModelIndex indexLowValue = model->index(row,2); 63 QModelIndex indexTempValue = model->index(row,3); 64 QModelIndex indexSampleData = model->index(row,4); 6566out<<indexId.data().toInt()<<"\t"<<indexHighValue.data().toDouble() 67 <<"\t"<<indexLowValue.data().toDouble()<<"\t"<<indexTempValue.data().toDouble() 68 <<"\t"<<indexSampleData.data().toString(); 69out<<"\n"; 70 } 71 file.close(); 72 } 73 } 74 }
原文:http://www.cnblogs.com/ch122633/p/7742482.html
内容总结
以上是互联网集市为您收集整理的qt中使用sqlite存储数据全部内容,希望文章能够帮你解决qt中使用sqlite存储数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。