关于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 ...
随机推荐
- maven mirror , profile , snapshot 和release
1. settings.xml 配置的mirror <mirrors> <mirror> <id>Nexus</id> <name>nexu ...
- rowspan和colspan的区别粗解
rowspan和colspan是我们初学HTML表格中会在做一些特殊表格中遇到.其常在td中添加. rowspan的作用是指定纵向所跨越单元格的行数. 如下效果. colspan的作用是指定单元格横向 ...
- Ubuntu上搭建Hadoop环境(单机模式+伪分布模式) (转载)
Hadoop在处理海量数据分析方面具有独天优势.今天花了在自己的Linux上搭建了伪分布模式,期间经历很多曲折,现在将经验总结如下. 首先,了解Hadoop的三种安装模式: 1. 单机模式. 单机模式 ...
- Flex的Number和Text
今天要说的问题不是Number和String转换的问题.而是使用时容易出的一些错误: public static function ToFixed(value:Number, digits:uint ...
- mybatis-mysql类型映射
JDBC Type Java Type CHAR String VARCHAR String LONGVARCHAR String NUMERIC java.math.BigDecimal DECIM ...
- 【Go】 Go 语言环境安装
安装环境/工具 1.Linux(CentOS 7.4版) 2.go1.11.2.linux-amd64.tar Go 语言环境安装 1.下载安装包 安装包下载地址为:https://golang.or ...
- hibernate配置文件 连接数据库
http://jingyan.baidu.com/album/0320e2c1d4dd0b1b87507b38.html?picindex=12
- PHP字符串函数运用小案例
$str = 'ZenD_CONTRollER_FronT'; //转换为Zend_Controller_Front //1.全部转换为小写 $str1 = strtolower($str); //2 ...
- FS210(cortex-A8)移植MT7601无线WIFI模块
准备:ubuntu 12.04 板子内核:3.0.2 交叉编译器:arm-cortex_a8-linux-gnueabi-gcc 所需资源下载:https://pan.baidu.com/s/1yWA ...
- Codeforces Round #542(Div. 2) CDE 思维场
C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...