PDA

View Full Version : QSqlQuery with MySQL performs twicely (SOLVED)



zzz9
19th August 2012, 11:08
Hi, I will be very glad to get a quick answer. I'm using QSqlQuery with QSqlDatabase with MySQL. I try to exec one query but client shows two entries in the table. Why?

#include <QtGui/QApplication>
#include "MainWindow.h"

#include <QMetaType>
#include <QTextCodec>

#include <QSqlDatabase>
#include <QSqlQuery>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("password0");
db.setDatabaseName("fitnesscenter");
db.open();

QSqlQuery query("insert into users(fullname) values ('Someone')");
query.exec();
}


mysql> delete from users;
Query OK, 2 rows affected (0.04 sec)

mysql> select * from users;
+----+----------+----------+
| id | fullname | password |
+----+----------+----------+
| 39 | Someone | NULL |
| 40 | Someone | NULL |
+----+----------+----------+
2 rows in set (0.00 sec)

mysql>

EDIT:

This post contains the right answer:
http://www.qtcentre.org/threads/4550...742#post206742
It is extremely unobvious!

ChrisW67
20th August 2012, 03:13
Firstly, I think you meant to link to a different thread.

Secondly, given that the behaviour of the constructor you used is documented QSqlQuery::QSqlQuery() I think it is obvious.