Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt sql query failed

Tags:

c++

mysql

qt

qtsql

query->prepare("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, ?, ?, ?, ?)");
query->bindValue(1, "source");
query->bindValue(2, "date");
query->bindValue(3, "headline");
query->bindValue(4, "body");

if (query->exec()) {
    tt << "Query success";
} else {
    qDebug() << db.lastError();
    return;
}

I'm using QMYSQL. And the error is

 QSqlError(-1, "", "") 

But

query->exec("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, 'google', 'may 0, 23', 'head', 'fjsdflksjdlkfdsjlfkjd');")

is working fine.

like image 546
Dewsworld Avatar asked Sep 10 '26 06:09

Dewsworld


1 Answers

It is hard to guess the problem. But one thing I could imagine and that definitely is wrong with your code is that field numbering for bindValue starts at 0 (see here).

Thus, I would suggest to try

query->prepare("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, ?, ?, ?, ?)");
query->bindValue(0, "source");
query->bindValue(1, "date");
query->bindValue(2, "headline");
query->bindValue(3, "body");

if (query->exec()) {
    tt << "Query success";
} else {
    qDebug() << db.lastError();
    return;
}
like image 105
Thilo Avatar answered Sep 12 '26 22:09

Thilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!