就关于qt连接使用sqlite折腾了一晚上.倒也不全是因为数据库连接的问题, 主要还是数据格式各自出问题.

原来的数据库是access, 为了导入linux下的sqlite, 我把其输出格式改成了xml文档. 然后在qt中对其进行解析.

  1. // QDomElement docElem = doc.documentElement();
  2. // //QDomNode n = docElem.firstChild();
  3. // QDomNodeList list = doc.elementsByTagName(QString("Row")).at(0).toElement().childNodes();
  4.  
  5. // QString s = "";
  6. // for(int i=0; i<list.length();i++){
  7. // s += list.at(i).toElement().text();
  8. // if(i!=list.length()-1){
  9. // s += ",";
  10. // }
  11. // }
  12. // qDebug()<<qPrintable(s);

以上获得的是表的字段, 通过获得的字符串我直接在命令行里创建了一个表.

然后就是数据的添加了.

首先新建一个connection.h头文件

  1. #ifndef CONNECTION_H
  2. #define CONNECTION_H
  3. #include <QMessageBox>
  4. #include <QSqlDatabase>
  5. #include <QSqlQuery>
  6. #include <QDebug>
  7.  
  8. static bool createConnection(){
  9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  10. db.setDatabaseName("/home/hao/Dangers/Danger.db");
  11.  
  12. if(!db.open()){
  13. qDebug() <<"aaaa";
  14. QMessageBox::critical(0,"Cannot open database",
  15. "unable to establish a database connection.", QMessageBox::Cancel);
  16. return false;
  17. }
  18.  
  19. return true;
  20. }
  21.  
  22. #endif // CONNECTION_H

 当然工程要加上

  1. QT += core gui sql xml
  1. 然后就是在main.cpp进行操作了
  1. #include<QApplication>
  2. #include<QSqlDatabase>
  3. #include<QDebug>
  4. #include <QString>
  5. #include<QStringList>
  6. #include <QtCore/QCoreApplication>
  7. #include <QtXml>
  8. #include "connection.h"
  9. #include <QVariant>
  10.  
  11. int main(int argc, char * argv[]){
  12.  
  13. QApplication a(argc,argv);
  14.  
  15. qDebug() << QDir::currentPath();
  16. QDomDocument doc;
  17. QFile file("../database/important.xml");
  18. if(!file.open(QIODevice::ReadOnly)) {
  19. qDebug() << "open failed";
  20. return ;
  21. }
  22. if(!doc.setContent(&file)){
  23. file.close();
  24. return ;
  25. }
  26.  
  27. file.close();
  28.  
  29. if(!createConnection()) {
  30. qDebug() <<"connection failed";
  31. return ;
  32. }
  33.  
  34. QSqlQuery query;
  35.  
  36. QDomElement docElem = doc.documentElement();
  37. QDomNodeList list = doc.elementsByTagName(QString("Row"));
  38.  
  39. for(int i=; i<list.length(); i++){
  40. QString s = "";
  41. int index = ;
  42. QDomNodeList attrs = list.at(i).childNodes();
  43. for(int j=; j<attrs.length(); j++){
  44. if(attrs.at(j).toElement().attribute(QString("ss:Index"))!=""){    //这里处理费了一阵功夫,因为这个xml文档中,当有些字段为空时,它会直接加一个index的标示符,而之前的就不生成节点了.
  45. while(index < attrs.at(j).toElement().attribute(QString("ss:Index")).toInt()){
  46. s = s+"\'" + "\'" + ",";
  47. index ++;
  48. }
  49.  
  50. }
  51. index ++;
  52. s += '\''+attrs.at(j).toElement().text()+'\'';
  53. if(index<=){
  54. s += ',';
  55. }
  56. }
  57. while(index <= ){    //一共有96个字段
  58. s = s+ "\'" + "\'";
  59. if(index !=){
  60. s+=",";
  61. }
  62. index++;
  63. }
  64. QString sr = "insert into tablename values("+s+")";
  65. query.exec(sr+";");
  66. qDebug() << "--------------------------------";
  67. qDebug() << qPrintable(sr);
  68.  
  69. }
  70. qDebug() << list.length();
  71.  
  72. return a.exec();
  73. }

/********2016.8.25更新********************/

研究了一上午, 写了一个连接数据库的模板类.

之后当然会有更多的扩充

  1. /*******connectdb.h***********/
  2. #ifndef CONNECTDB_H
  3. #define CONNECTDB_H
  4.  
  5. #include <QSqlDatabase>
  6. #include <QSqlQuery>
  7. #include <QString>
  8. #include <QMessageBox>
  9. #include <QComboBox>
  10.  
  11. class ConnectDB
  12. {
  13. public:
  14. ConnectDB(QString dbname="database.db
  15. ");
  16. //ConnectDB();
  17. ~ConnectDB();
  18.  
  19. QSqlDatabase db;
  20. QSqlQuery select(QString sql);
  21.  
  22. void getSubstance(QComboBox *box);
  23. };
  24.  
  25. #endif // CONNECTDB_H
  26.  
  27. /****************connectdb.cpp*************/
  28. #include "connectdb.h"
  29. #include <QDebug>
  30.  
  31. ConnectDB::ConnectDB(QString dbname)
  32. {
  33. db = QSqlDatabase::addDatabase("QSQLITE");
  34. db.setDatabaseName(dbname);
  35. if( !db.open() ){
  36. QMessageBox::critical(, "Cannot open database",
  37. "Unable to establish a database connection.", QMessageBox::Cancel);
  38. }
  39. }
  40.  
  41. ConnectDB::~ConnectDB(){
  42.  
  43. }
  44.  
  45. QSqlQuery ConnectDB::select(QString sql){
  46. QSqlQuery query;
  47. query.exec(sql);
  48. return query;
  49. }
  50.  
  51. //这个函数作用是获取数据库中的物质名称填充到组件的comboBox中去
  52. void ConnectDB::getSubstance(QComboBox *box){
  53. QString sql = "select 中文名称 from tablename order by id";
  54. QSqlQuery query = this->select(sql);
  55. while(query.next()){
  56. QString item = query.value().toString();
  57. box->addItem(QString(item));
  58. }
  59. }
  60.  
  61. /**********调用方法************/
  62.  
  63. #include "connectdb.h"
  64.  
  65. ......
  66.  
  67. ConnectDB *db = new ConnectDB();
  68.  
  69. ComboBox *nameSelect = new QComboBox;
  70. 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. JDBC连接MySql,配置url报错

    使用JDBC连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one ...

  2. Windows-universal-samples学习笔记系列五:Custom user interactions

    Custom user interactions Basic input Complex inking Inking Low latency input Simple inking Touch key ...

  3. 【Web】网页字体图标的使用

    字体图标介绍 网页中图片有很多优点,但也有很多缺点,会增加文件的大小以及增加http请求.这时候就需要用的字体图标(iconfont).字体图标的优点,可以跟图片一样改变透明度.旋转等,本质上是文字, ...

  4. Desktop Central帮助您升级Windows 10,获取更新的五大增强功能

  5. 812. Largest Triangle Area

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  6. 2019.01.17 bzoj2753: [SCOI2012]滑雪与时间胶囊(最小生成树)

    传送门 最小生成树菜题. 题意:给出一些有向边,问有向的最小生成树. 思路:先dfsdfsdfs一把所有有用的边都存起来,然后按终点点权为第一关键字,边权为第二关键字给边排序保证最小生成树的合法性,排 ...

  7. C# AOP框架入门(转)

    出处:https://www.cnblogs.com/isaboy/p/Csharp_AOP_Log.html AOP面向切面编程(Aspect Oriented Programming),是通过预编 ...

  8. 从模板驱动文件ins生成cls文件

    在当前目录下,启动cmd程序,输入以下指令: latex acmart.ins

  9. Arria10收发器校正

    收发器的模拟和数字部分都需要校正来补偿过程,电压和温度(PTV)带来的变化. Arria10使用PreSICE来执行校正过程.   校正主要包括上电校正和用户校正两方面: 上电校正在器件上电时自动执行 ...

  10. java项目显示红叉,程序却没有错误

    转 http://blog.sina.com.cn/s/blog_825b7d7c0102w7rq.html (2016-07-02 11:38:38)   分类: javaWeb 电脑换了不同版本的 ...