Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How can I dump database tables with Qt?
QtWS25 Last Chance

[SOLVED] How can I dump database tables with Qt?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 8.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    panosk
    wrote on 30 Jun 2013, 14:24 last edited by
    #1

    Hi,

    Is there a way to dump tables from an Sqlite database (or the whole database) into a text file using Qt's QSqlDatabase, QSqlQuery, and friends? This file will be used to feed data to another database system.

    Thanks in advance.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SergioDanielG
      wrote on 30 Jun 2013, 14:32 last edited by
      #2

      Hi panosk.
      Maybe you can see how "qsqlitebrowser":http://sqlitebrowser.sourceforge.net/ do it.
      From qsqlitebrowser

      bq. What it is
      SQLite Database Browser is a freeware, public domain, open source visual tool used to create, design and edit database files compatible with SQLite.

      • Import and export databases from/to SQL dump files
        bq.

      Hope it's util.

      Regards.

      www.ftatv.com.ar El foro argentino de la TV libre

      1 Reply Last reply
      0
      • P Offline
        P Offline
        panosk
        wrote on 30 Jun 2013, 14:36 last edited by
        #3

        Hi SergioDanielG,

        Thanks for the link, but I need to do this programmatically from within my code. As a last resort, I may try to create a QProcess that will run the sqlite3 client utility and then use ".dump", but I would like to avoid it if there is another way.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SergioDanielG
          wrote on 30 Jun 2013, 15:48 last edited by
          #4

          My idea is: Download source and read the code of qsqlitebrowser and watch how qsqlitebrowser dump tables.
          Or use "QSqlDatabase":http://qt-project.org/doc/qt-5.0/qtsql/qsqldatabase.html , "QSqlQuery":http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html , etc and sql commands like
          @
          select * from sqlite_master;
          @

          to search database's tables structure, then filter data of tables and then write to a file.
          Hope it's util.
          Regards.

          www.ftatv.com.ar El foro argentino de la TV libre

          1 Reply Last reply
          0
          • P Offline
            P Offline
            panosk
            wrote on 30 Jun 2013, 23:00 last edited by
            #5

            Hi,

            I finally came up with this solution which seems to work for my purposes and the resulting file imports fine into PostgreSQL. Note that I don't need the schema, only the data, but it should be easy to include schemas too. As the code is very fresh, any comments, suggestions, or improvements are most welcome.

            @
            void DbManager::exportTables()
            {
            QHash<QString,QStringList> tablesWithFields; //It holds the table name and its fields
            QStringList tables = sourceDb.tables();
            QSqlQuery query(sourceDb);
            foreach(QString table,tables) {
            query.exec(QString("PRAGMA TABLE_INFO(%1)").arg(table));
            QStringList fields;
            while(query.next()) {
            fields << query.value(1).toString();
            }
            tablesWithFields.insert(table,fields);
            }
            QFile f(QDir::homePath() + "/myDump.sql");
            f.open(QIODevice::Append | QIODevice::Text);
            QTextStream streamer(&f);

            /*If constraints can't be dropped in the target database, some reordering of the INSERT statements may be needed
            */
            QStringList sortedTables = tablesWithFields.keys();
            sortedTables.move(sorted.indexOf("table1"),0);
            ...

            streamer << "BEGIN;\n";
            foreach(const QString &table,sortedTables) {
                if(table=="sqlite_sequence" /*|| table=="table4", etc*/) continue;
                QString statement = QString("INSERT INTO %1 VALUES('").arg(table);
                QStringList fields = tablesWithFields.value(table);
                QString fieldsString = fields.join(",");
                query.exec&#40;QString("SELECT %1 FROM %2"&#41;.arg(fieldsString).arg(table));
                if(!query.next()) continue;
                query.previous();
                while(query.next()) {
                    for(int i=0; i < fields.size(); ++i) {
                        QString value = query.value(i).toString();
                        value.replace("'","''"); //Handle single quotes inside strings
                        if(value.isEmpty()) {
                            value = "NULL";
                            statement.chop(1); //NULL should not appear inside quotes
                            statement.append(value+",'");
                        } else {
                        statement.append(value+"','");
                        }
                    }
                    statement.chop(2); //Remove comma and single quote from the end of value group
                    statement.append("),('"); //Close the value group and start a new one
                }
                statement.chop(3); //Remove comma, opening parenthesis and single quote from the end
                streamer << statement << ";\n"; //Complete the INSERT statement
            }
            streamer << "COMMIT;";
            f.close();
            

            }
            @

            And then I batch execute the sql file like this:

            @
            ...
            exportTables();
            QSqlQuery query(targetDb);
            QFile f(QDir::homePath()+"/myDump.sql");
            f.open(QIODevice::ReadOnly | QIODevice::Text);
            if(!query.exec(f.readAll()))
            qCritical() << "Can't execute sql file: " << query.lastError().text();
            @

            1 Reply Last reply
            0

            4/5

            30 Jun 2013, 15:48

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved