就关于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的更多相关文章

  1. 在qt中用tcp传输xml消息

    在qt中用tcp传输xml消息 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:Qt5 3.1.2 说明: 在tcp上 ...

  2. Qt 操作SQLite数据库

    项目中通常需要采用各种数据库(如 Qracle.SQL Server.MySQL等)来实现对数据的存储.查询等功能.下面讲解如何在 Qt 中操作 SQlite 数据库. 一.SQLite 介绍 Sql ...

  3. Qt Write and Read XML File 读写XML文件

    在Qt中,我们有时候需要把一些参数写入xml文件,方便以后可以读入,类似一种存档读档的操作,例如,我们想生成如下的xml文件: <?xml version="1.0" enc ...

  4. Qt读取JSON和XML数据

    QJSON JSON(JavaScript Object Notation)是一个轻量级的数据交换格式; 可以将数据以name/value的形式任意组合; QJson 是一个基于Qt的库, 将JSON ...

  5. Qt之QDomDocument操作xml文件-模拟ini文件存储

    一.背景 不得不说Qt是一个很强大的类库,不管是做项目还是做产品,Qt自身封装的东西就已经非常全面了,我们今天的这篇文章就是模拟了Qt读写ini文件的一个操作,当然是由于一些外力原因,我们决定自己来完 ...

  6. QT使用SQLite

    在QT的widget中用tableview显示sqlite数据库表中的内容. 用QTcreator创建一个基于Widget类的窗口,再拖一个tableview到widget中,保存. 1.在widge ...

  7. qt中用tcp传输xml消息 good

    本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.   环境: 主机:WIN7 开发环境:Qt5 3.1.2 说明: 在tcp上传输xml消息. 协议格式如 ...

  8. Qt基于sqlite数据库的管理小软件

    闲来无事,写了一个基于sqlite的数据库管理小软件. 先上图 中心思想就是: 创建一个数据库 然后每一个分组对应一个数据表 然后遍历该数据表.将名字以treewidgetItem显示出来.添加删除实 ...

  9. QT中Sqlite的使用

    环境: 静态编译过sqlite 步骤: 1.C++链接器中加入Sqlite.lib,然后在测试一下是否能正常加载Sqlite驱动 #include<QtPlugin> Q_IMPORT_P ...

随机推荐

  1. No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal

    No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in t ...

  2. C语言基础第四次作业

    题目7-2,九九乘法表 1.实验代码: #include<stdio.h> int main() { int N, i, j, q; scanf("%d",&N ...

  3. syslog、日志服务器安装、卸载详解、如何安装和卸载EventLog Analyzer

  4. 2019.01.19 codeforces893F.Subtree Minimum Query(线段树合并)

    传送门 线段树合并菜题. 题意简述:给一棵带点权的有根树,多次询问某个点ppp子树内距离ppp不超过kkk的点的点权最小值,强制在线. 思路: 当然可以用dfsdfsdfs序+主席树水过去. 然而线段 ...

  5. 2018.11.24 poj2774Long Long Message(后缀数组)

    传送门 实际上可以用后缀自动机秒掉 当然后缀数组也挺好写. 我们将两个字符串接在一起,为了方便中间用一个特殊字符连接. 然后对新字符串求heightheightheight数组. 求出来之后对所有满足 ...

  6. java正则表达式笔记

    import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntax ...

  7. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) D. Minimum path(字典序)

    https://codeforces.com/contest/1072/problem/D 题意 给你一个n*n充满小写字母的矩阵,你可以更改任意k个格子的字符,然后输出字典序最小的从[1,1]到[n ...

  8. 类型转化&WCF不同binding的区别

    需要使用队列时并且涉及多线程时使用ConcurrentQueue 这个性内比自己使用Queue并且配合lock要好很多 calcFactory = new ChannelFactory<ICal ...

  9. CString成员函数详解[转]

    1.构造函数(常用) CString( const unsigned char* psz );      例:char s[]="abcdef";              cha ...

  10. C# 中的委托(Delegate)

    委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托(Delegate)特别用于实现事件和回调方法.所有的委托(Delegate)都派生自 System.D ...