就关于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. Web应用获取文件路径的方法

    拥有 HttpServletRequest req 对象 req.getSession().getServletContext().getRealPath("/")   ----- ...

  2. response输出随机图片、定时刷新网页

    第一招:利用response向浏览器输出图片: //获取验证码 在<img />标签内的src属性设为请求路径/verifyCode?goodsId=xxx&token=xxxxx ...

  3. mybatis 使用merge into

    前一篇博客,oracle的merge into语法 : oracle merge into语法 mybatis 使用merge into,跟一般的update写法相同: <update id=& ...

  4. 2018.11.09 bzoj2165: 大楼(倍增+floyd)

    传送门 先倍增出iii使得2i2^i2i时间时刚好有每个点能够到mmm层及以上. 然后就可以用floyd+floyd+floyd+倍增求出刚好不超过mmm层的时间,最后再补一层就行了. 代码: #pr ...

  5. js动态删除某一行,内容超出单元格后超出的部分用省略号代替

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <s ...

  6. HttpFilter

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  7. VB连接MYSQL数据的方法

    原文链接:http://hanbaohong.iteye.com/blog/704800 第一步:上网http://dev.mysql.com/downloads/connector/odbc/下载m ...

  8. 【1】jQuery异步(Ajax)操作之JSONP [转]

     前言: 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域 ...

  9. 基于脚本的nodemanager管理器

    Step 6: Configure Node Manager on the Managed Servers虚拟机环境: 管理服务器IP:192.168.227.10  AdminServer 受管服务 ...

  10. Django工程搭建

    -----环境安装 1.创建虚拟环境 mkvirtualenv django_py3_1.11 -p python3   2.安装django pip install django==1.11.11 ...