关于QT和SQLite以及XML
就关于qt连接使用sqlite折腾了一晚上.倒也不全是因为数据库连接的问题, 主要还是数据格式各自出问题.
原来的数据库是access, 为了导入linux下的sqlite, 我把其输出格式改成了xml文档. 然后在qt中对其进行解析.
- // QDomElement docElem = doc.documentElement();
- // //QDomNode n = docElem.firstChild();
- // QDomNodeList list = doc.elementsByTagName(QString("Row")).at(0).toElement().childNodes();
- // QString s = "";
- // for(int i=0; i<list.length();i++){
- // s += list.at(i).toElement().text();
- // if(i!=list.length()-1){
- // s += ",";
- // }
- // }
- // qDebug()<<qPrintable(s);
以上获得的是表的字段, 通过获得的字符串我直接在命令行里创建了一个表.
然后就是数据的添加了.
首先新建一个connection.h头文件
- #ifndef CONNECTION_H
- #define CONNECTION_H
- #include <QMessageBox>
- #include <QSqlDatabase>
- #include <QSqlQuery>
- #include <QDebug>
- static bool createConnection(){
- QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
- db.setDatabaseName("/home/hao/Dangers/Danger.db");
- if(!db.open()){
- qDebug() <<"aaaa";
- QMessageBox::critical(0,"Cannot open database",
- "unable to establish a database connection.", QMessageBox::Cancel);
- return false;
- }
- return true;
- }
- #endif // CONNECTION_H
当然工程要加上
- QT += core gui sql xml
- 然后就是在main.cpp进行操作了
- #include<QApplication>
- #include<QSqlDatabase>
- #include<QDebug>
- #include <QString>
- #include<QStringList>
- #include <QtCore/QCoreApplication>
- #include <QtXml>
- #include "connection.h"
- #include <QVariant>
- int main(int argc, char * argv[]){
- QApplication a(argc,argv);
- qDebug() << QDir::currentPath();
- QDomDocument doc;
- QFile file("../database/important.xml");
- if(!file.open(QIODevice::ReadOnly)) {
- qDebug() << "open failed";
- return ;
- }
- if(!doc.setContent(&file)){
- file.close();
- return ;
- }
- file.close();
- if(!createConnection()) {
- qDebug() <<"connection failed";
- return ;
- }
- QSqlQuery query;
- QDomElement docElem = doc.documentElement();
- QDomNodeList list = doc.elementsByTagName(QString("Row"));
- for(int i=; i<list.length(); i++){
- QString s = "";
- int index = ;
- QDomNodeList attrs = list.at(i).childNodes();
- for(int j=; j<attrs.length(); j++){
- if(attrs.at(j).toElement().attribute(QString("ss:Index"))!=""){ //这里处理费了一阵功夫,因为这个xml文档中,当有些字段为空时,它会直接加一个index的标示符,而之前的就不生成节点了.
- while(index < attrs.at(j).toElement().attribute(QString("ss:Index")).toInt()){
- s = s+"\'" + "\'" + ",";
- index ++;
- }
- }
- index ++;
- s += '\''+attrs.at(j).toElement().text()+'\'';
- if(index<=){
- s += ',';
- }
- }
- while(index <= ){ //一共有96个字段
- s = s+ "\'" + "\'";
- if(index !=){
- s+=",";
- }
- index++;
- }
- QString sr = "insert into tablename values("+s+")";
- query.exec(sr+";");
- qDebug() << "--------------------------------";
- qDebug() << qPrintable(sr);
- }
- qDebug() << list.length();
- return a.exec();
- }
/********2016.8.25更新********************/
研究了一上午, 写了一个连接数据库的模板类.
之后当然会有更多的扩充
- /*******connectdb.h***********/
- #ifndef CONNECTDB_H
- #define CONNECTDB_H
- #include <QSqlDatabase>
- #include <QSqlQuery>
- #include <QString>
- #include <QMessageBox>
- #include <QComboBox>
- class ConnectDB
- {
- public:
- ConnectDB(QString dbname="database.db
- ");
- //ConnectDB();
- ~ConnectDB();
- QSqlDatabase db;
- QSqlQuery select(QString sql);
- void getSubstance(QComboBox *box);
- };
- #endif // CONNECTDB_H
- /****************connectdb.cpp*************/
- #include "connectdb.h"
- #include <QDebug>
- ConnectDB::ConnectDB(QString dbname)
- {
- db = QSqlDatabase::addDatabase("QSQLITE");
- db.setDatabaseName(dbname);
- if( !db.open() ){
- QMessageBox::critical(, "Cannot open database",
- "Unable to establish a database connection.", QMessageBox::Cancel);
- }
- }
- ConnectDB::~ConnectDB(){
- }
- QSqlQuery ConnectDB::select(QString sql){
- QSqlQuery query;
- query.exec(sql);
- return query;
- }
- //这个函数作用是获取数据库中的物质名称填充到组件的comboBox中去
- void ConnectDB::getSubstance(QComboBox *box){
- QString sql = "select 中文名称 from tablename order by id";
- QSqlQuery query = this->select(sql);
- while(query.next()){
- QString item = query.value().toString();
- box->addItem(QString(item));
- }
- }
- /**********调用方法************/
- #include "connectdb.h"
- ......
- ConnectDB *db = new ConnectDB();
- ComboBox *nameSelect = new QComboBox;
- db->getSubstance(nameSelect);
关于QT和SQLite以及XML的更多相关文章
- 在qt中用tcp传输xml消息
在qt中用tcp传输xml消息 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:Qt5 3.1.2 说明: 在tcp上 ...
- Qt 操作SQLite数据库
项目中通常需要采用各种数据库(如 Qracle.SQL Server.MySQL等)来实现对数据的存储.查询等功能.下面讲解如何在 Qt 中操作 SQlite 数据库. 一.SQLite 介绍 Sql ...
- Qt Write and Read XML File 读写XML文件
在Qt中,我们有时候需要把一些参数写入xml文件,方便以后可以读入,类似一种存档读档的操作,例如,我们想生成如下的xml文件: <?xml version="1.0" enc ...
- Qt读取JSON和XML数据
QJSON JSON(JavaScript Object Notation)是一个轻量级的数据交换格式; 可以将数据以name/value的形式任意组合; QJson 是一个基于Qt的库, 将JSON ...
- Qt之QDomDocument操作xml文件-模拟ini文件存储
一.背景 不得不说Qt是一个很强大的类库,不管是做项目还是做产品,Qt自身封装的东西就已经非常全面了,我们今天的这篇文章就是模拟了Qt读写ini文件的一个操作,当然是由于一些外力原因,我们决定自己来完 ...
- QT使用SQLite
在QT的widget中用tableview显示sqlite数据库表中的内容. 用QTcreator创建一个基于Widget类的窗口,再拖一个tableview到widget中,保存. 1.在widge ...
- qt中用tcp传输xml消息 good
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:Qt5 3.1.2 说明: 在tcp上传输xml消息. 协议格式如 ...
- Qt基于sqlite数据库的管理小软件
闲来无事,写了一个基于sqlite的数据库管理小软件. 先上图 中心思想就是: 创建一个数据库 然后每一个分组对应一个数据表 然后遍历该数据表.将名字以treewidgetItem显示出来.添加删除实 ...
- QT中Sqlite的使用
环境: 静态编译过sqlite 步骤: 1.C++链接器中加入Sqlite.lib,然后在测试一下是否能正常加载Sqlite驱动 #include<QtPlugin> Q_IMPORT_P ...
随机推荐
- JDBC连接MySql,配置url报错
使用JDBC连接MySql时出现:The server time zone value '�й���ʱ��' is unrecognized or represents more than one ...
- Windows-universal-samples学习笔记系列五:Custom user interactions
Custom user interactions Basic input Complex inking Inking Low latency input Simple inking Touch key ...
- 【Web】网页字体图标的使用
字体图标介绍 网页中图片有很多优点,但也有很多缺点,会增加文件的大小以及增加http请求.这时候就需要用的字体图标(iconfont).字体图标的优点,可以跟图片一样改变透明度.旋转等,本质上是文字, ...
- Desktop Central帮助您升级Windows 10,获取更新的五大增强功能
- 812. Largest Triangle Area
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 2019.01.17 bzoj2753: [SCOI2012]滑雪与时间胶囊(最小生成树)
传送门 最小生成树菜题. 题意:给出一些有向边,问有向的最小生成树. 思路:先dfsdfsdfs一把所有有用的边都存起来,然后按终点点权为第一关键字,边权为第二关键字给边排序保证最小生成树的合法性,排 ...
- C# AOP框架入门(转)
出处:https://www.cnblogs.com/isaboy/p/Csharp_AOP_Log.html AOP面向切面编程(Aspect Oriented Programming),是通过预编 ...
- 从模板驱动文件ins生成cls文件
在当前目录下,启动cmd程序,输入以下指令: latex acmart.ins
- Arria10收发器校正
收发器的模拟和数字部分都需要校正来补偿过程,电压和温度(PTV)带来的变化. Arria10使用PreSICE来执行校正过程. 校正主要包括上电校正和用户校正两方面: 上电校正在器件上电时自动执行 ...
- java项目显示红叉,程序却没有错误
转 http://blog.sina.com.cn/s/blog_825b7d7c0102w7rq.html (2016-07-02 11:38:38) 分类: javaWeb 电脑换了不同版本的 ...