0

Using Qt 4.8.6 and clang++ x86_64-apple-darwin14.3.0 on my late-2014 MacBook Pro I find that QsqlTableModel::insertRecord() fails to write the target MYSQL database: myDB while constructing the QSqlDatbase: myDB using a non-default connection such as:

db = QSqlDatabase::addDatabase("QMYSQL","Connection1");

and insertRecord(-1,myRecord) evaluates to false.

Instead, if I use the usual default connection:

db = QSqlDatabase::addDatabase("QMYSQL");

the database is appended successfully as:

mysql> select * from myTable;
+------+------+
| col1 | col2 |
+------+------+
|    1 |    2 |
|    5 |    6 |
+------+------+
2 rows in set (0.00 sec)

The usual recommendation to accommodate multiple connection names is to construct QSqlQuery with QSqlDatabase as:

QSqlQuery query(db)

that evidently doesn't seem to help in this case. Can you point out what's missing?

The small amount of runnable code that exemplifies the above is below:

testdb.h

#ifndef TESTDB_H
#define TESTDB_H

#include <QtSql>
#include <QtCore>


class TestDB
{
public:
    TestDB();
    ~TestDB();
    void dbInit();
    void appendDB(const int &col1, const int &col2);
private:
    QSqlDatabase db;
    QSqlTableModel *model;
};

#endif // TESTDB_H

testdb.cpp

#include "testdb.h"

TestDB::TestDB()
{
    dbInit();
}

TestDB::~TestDB()
{

}

void TestDB::dbInit()
{
    // database connection
    db = QSqlDatabase::addDatabase("QMYSQL","Connection1");
    db.setDatabaseName("myDB"); 
    db.setUserName("root");
    db.setPassword("");
    db.setHostName("localhost");
    db.setConnectOptions();
    if (!db.open())
    {
        qDebug() << "Database error occurred in class: stockinfo" << db.lastError().text();
    }
    QSqlQuery query(db);

    // create database named transactions
    query.exec("CREATE DATABASE IF NOT EXISTS myDB;");
    query.exec("USE myDB;");
    query.exec("CREATE TABLE IF NOT EXISTS myTable("
                   "col1 INT,"
                   "col2 INT"
                   ");");
    model = new QSqlTableModel;
    model->setTable("myTable");
    model->select();
}

void TestDB::appendDB(const int &col1, const int &col2)
{
    QSqlRecord myRecord;
    myRecord.append(QSqlField("col1", QVariant::Int));
    myRecord.append(QSqlField("col2", QVariant::Int));

    myRecord.setValue("col1",col1);
    myRecord.setValue("col2",col2);

    model->insertRecord(-1,myRecord);
    qDebug() << "model->insertRecord(-1,myRecord) = " << model->insertRecord(-1,myRecord);
    qDebug() << "Last Database error: " << db.lastError().text();
}

main.cpp

#include <QCoreApplication>
#include <testdb.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    TestDB myTestDB;
    myTestDB.appendDB(1,2);
    myTestDB.appendDB(5,6);
    return a.exec();
}

dbTest.pro

QT       += core sql

QT       -= gui

TARGET = dbTest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    testdb.cpp

HEADERS += \
    testdb.h
3
  • I googled my way to (qtcentre.org/threads/…), that was 1 search of "QSqlDatabase::addDatabase" + 1 click (on the hit saying "SOLVED:") + 1 scroll. Hopefully it helps. In short, when you use a connection name for opening the database, you have to specify that connection name in the query. Commented May 21, 2015 at 16:07
  • Thanks, but as I had already pointed out I do pass the constructor for QSqlQuery with QSqlDatabase. Nevertheless, the approach suggested in your link produces the same problem. Commented May 21, 2015 at 16:18
  • Sorry, I only see the name "Connection1" once in the posted code. Maybe I'm equally blind on one eye as on the other. Commented May 21, 2015 at 16:23

1 Answer 1

1

When you are creating QSqlTableModel (testdb.cpp:35) you do not specify any database connection, therefore your model wraps default connection. This is probably not what you wanted, not tested. See documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.