PDA

View Full Version : QSqlQuery problem (insert once but create two rows!!!)



ditsikts
26th October 2011, 11:34
When I insert a name, create two rows

Here is insert statement

void wmtDB::addEmployee(QString name)
{
QSqlQuery qry;
qry.prepare("INSERT INTO employeestuff (name) VALUES (:name)");
qry.bindValue(":name", name );
if (!qry.exec())
qFatal("Failed to add employee");
}

and here the call

void MainWindow::buttonOnClick()
{
employees.addEmployee(ui->lineEdit->text());
updateEmployees();
}

if db is empty and i run it one time with parameter "john"
the "SELECT * FROM nameTable"
will return
1 "john"
2 "john"

please help me

nix
26th October 2011, 13:17
Connection between your button and your slot is done two times, so your slots is call twice. There is one connect in your ui add by QtDesigner probably (pass in signal/slot view to see it), and one in the constructor.

ditsikts
26th October 2011, 13:50
thank you, I fix it :D

DmitryNik
26th October 2011, 15:43
For reason of avoiding creating a new thread. Could that line of code "connect(this, SIGNAL(senddata(const QString &)), server, SIGNAL(getdata(const QString &)));" cause the problem, which was described above?

here is the code, what I have in the server-class:

qDebug() << "name: " + name << "staus: " + status << "pass: " + pass;
bool ok;
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "db");
db.setHostName(this->dbAddress);
db.setPort(this->dbPort);
db.setDatabaseName(this->dbName);
db.setUserName(this->AdminName);
db.setPassword(this->AdminPass);
ok = db.open();
if(ok)
{
QSqlQuery query("INSERT INTO people(name, status, pass) VALUES('" + name + "', '"
+ status + "', '" + pass + "')", db);
query.exec();
query.clear();
}
db.close();
}
QSqlDatabase::removeDatabase("db");

Output is:

"name: user" "staus: user" "pass: 12dea96fec20593566ab75692c9949596833adc9"

In my humble opinion, if I got only line with names, passes and statuses, it doesn't. But still I've got every time two new lines in the database table... Where can be a problem?

stampede
26th October 2011, 16:16
If you are afraid that connection has been made multiple times, use Qt::UniqueConnection

connect(this, SIGNAL(senddata(const QString &)), server, SIGNAL(getdata(const QString &)),Qt::UniqueConnection);

DmitryNik
26th October 2011, 16:57
If you are afraid that connection has been made multiple times, use Qt::UniqueConnection

connect(this, SIGNAL(senddata(const QString &)), server, SIGNAL(getdata(const QString &)),Qt::UniqueConnection);

Thank you for the answer. I tried everything. But still I get every time 2 new rows instead of one in the database's table. Using of qDebug() shows, that slot was called once. How it possible, that we have two new lines?

Oh... I got it... I need to delete exec()...

ChrisW67
27th October 2011, 04:55
The QSqlQuery constructor you are using will execute the query if it is not empty so you do not need to exec() again. However, you should not build query SQL by pasting together string fragments. You should use QSqlQuery::prepare(), QSqlQuery::bindValue(), and QSqlQuery::exec() to help avoid user-induced crashes and security holes: SQL Injection (http://www.wikipedia.org/wiki/SQL_injection)