Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-30205

QSqlDatabase/QSqlQuery inserts double records on SQLite 3 database

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: Not Evaluated Not Evaluated
    • None
    • 5.0.1
    • SQL Support
    • None

      When inserting a single record into a table, I see that the record was added twice. This is a BIG issue as I cannot at this point store data correctly into a SQLite3 database.

      The following example produces this error:

      #include <QCoreApplication>
      #include <QSqlDatabase>
      #include <QSqlQuery>
      #include <QSqlRecord>
      #include <QSqlError>
      #include <QString>
      #include <QVariant>

      #include <iostream>

      using namespace std;

      void execSql(QString sql, QSqlDatabase db);
      void showRecordCount(QSqlDatabase db);

      int main(int argc, char *argv[]) {
      QCoreApplication runtime(argc, argv);

      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "db1");
      db.setDatabaseName(QString("%1/db1.db").arg(QCoreApplication::applicationDirPath()));

      if (db.open())

      { cout << "Database open!" << endl; execSql("CREATE TABLE IF NOT EXISTS Table1 (Field1 TEXT)", db); showRecordCount(db); execSql("INSERT INTO Table1 (Field1) VALUES (\'value1\')", db); showRecordCount(db); db.close(); cout << "Database closed!" << endl; }

      else
      cout << "Database not open! " << db.lastError().text().toStdString() << endl;

      return 0;
      }

      void execSql(QString sql, QSqlDatabase db) {
      QSqlQuery cmd(sql, db);
      cmd.exec();
      }

      void showRecordCount(QSqlDatabase db) {
      QSqlQuery qry("SELECT count(1) FROM Table1", db);

      if (qry.exec())

      { if (qry.next()) cout << "Record Count: " << qry.record().value(0).toULongLong() << endl; }

      }

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            mabrand Mark Brand
            polla_tosen@yahoo.com Polla Tosen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes