4

i'm trying to connect to a sql database with the Qt-Framework.

Unfortunately db.open() always returns true (you can set any password, hostname, etc...), despite no connection is established(?). I derive that from the query not having any effect on the database.

I'm using LAMPP on Ubuntu 14.04.

I've got the following code:

#include "mainwindow.h"
#include "ui_mainwindow.h" 
#include <QApplication>
#include <QSql>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>


void MainWindow::on_ButtonSQL_clicked()
{

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "con1");
    db.setHostName("localhost");
    db.setDatabaseName("kinectshop2015");
    db.setUserName("root");
    db.setPassword("");

    QMessageBox msgBox;

    if (db.open()) {
        msgBox.setText("Yay! Your database host is "+db.hostName()+" .\n"+" The name of the database is "+db.databaseName()+".");
        msgBox.exec();
    }

    QSqlQuery query;
    query.exec("INSERT INTO users (id, username, balance, isAdmin)" "VALUES(3, 'somebody', 10000, 1)");
}
Alex
  • 751
  • 1
  • 6
  • 34
  • 3
    I guess the QSQLITE driver just ignores username and password since it does [not support those features](https://github.com/radekp/qt/blob/master/src/sql/drivers/sqlite/qsql_sqlite.cpp#L523). – m.s. May 09 '15 at 11:40
  • Yay, I was able to resolve my problem using the QMYSQL driver! On Ubuntu i needed to install the driver with: sudo apt-get install libqt5sql5-mysql – Alex May 09 '15 at 13:48
  • If this indeed solves the problem you should add it as an answer and mark the answer as accepted to make it visible that your problem is solved. http://stackoverflow.com/help/self-answer – Erik May 09 '15 at 13:51

2 Answers2

2

One problem is that you are specifying a connectionName.

QSqlDatabase QSqlDatabase::addDatabase(const QString & type, const QString & connectionName = QLatin1String( defaultConnection ))

If connectionName is not specified, the new connection becomes the default connection

Given that your db connection is not the default connection, you need to tell the QSqlQuery which db to use, i.e.

  QSqlQuery query(db);
  query.exec("INSERT INTO users (id, username, balance, isAdmin)" "VALUES(3, 'somebody', 10000, 1)");

The other 'problem' is that with sqlite you always create a new database by the call you are making, thus the problem is not that the DB is not open but that its contents are not right (since it will be empty if you specify a non existing filename). If I take your code and add

   qDebug() << query.lastError();

after the query.exec it rightfully complains that

QSqlError(1, "Unable to execute statement", "no such table: users")

If I include a statement to create the table before the insert query

QSqlQuery createTable("create table users ( id INTEGER, username TEXT, balance REAL, isAdmin INTEGER)",db);
createTable.exec();

I correctly get the table in the newly create db file and it has the entry that you are trying to insert.

Does this help?

Erik
  • 2,137
  • 3
  • 25
  • 42
  • Thank you for this explanation. What i actually tried to achieve was to insert an exntry into an **existing** database, rather than creating a new one. I only used the QSQLITe driver, because if I use QMYSQL it says i can't load that one. – Alex May 09 '15 at 13:27
  • @Alex what is the exact error you are getting in that case? – Erik May 09 '15 at 13:33
  • &"warning: GDB: Failed to set controlling terminal: Unpassender IOCTL (I/O-Control) f\303\274r das Ger\303\244t\n" QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE false QSqlQuery::exec: database not open – Alex May 09 '15 at 13:40
  • either you don't have mysql or you are missing the qt driver. Did you compile qt yourself or did you install it? What system type are you on? – Erik May 09 '15 at 13:43
  • possibly related: http://stackoverflow.com/questions/4479551/how-to-make-qt-aware-of-the-qmysql-driver – Erik May 09 '15 at 13:45
0

Here is what solved my problem. I used the driver QMYSQL instead of QSQLITE. I had only used the latter, because the first one couldn't be loaded. On Ubuntu & QT5 simply type sudo apt-get install libqt5sql5-mysql to resolve the problem.

Alex
  • 751
  • 1
  • 6
  • 34