Solved QSqlError.number() always returns -1
-
Hello,
I have an issue when executing sql.
QSqlError always returns -1 and text() is always "Parameter count mismatch", regardless the error I try to trigger.I use QSQLITE as driver.
Anyone can help ?
Thanks.
-
Hi,
Please show the code you are using as well as the query you are trying to execute.
-
Hello @SGaist
Here is the code :int Project::insert() { QSqlQuery query = DbConnector::getQuery(); query.prepare("INSERT INTO project(name, description, color) VALUES(:n, :d, :c)"); query.bindValue(":n", name); query.bindValue(":d", description); query.bindValue(":c", color); if(!query.exec()) { qDebug() << query.lastError().number(); } return query.lastInsertId().toInt(); }
I intentionnally droped the table, so I expect to see a kind of "No such table" or something like that.
-
@chraz What happens if you try to do the same on SQL console?
-
INSERT INTO project(name, description, color) VALUES("P1", "D1", "#000000");
Got error :
Error while executing SQL query on database 'todos': no such table: project
-
Which version of Qt are you using ?
-
Desktop Qt 5.2.0 MinGW 32bit
-
Did you check the return of the query.prepare call ?
Perhaps you are not connected to the database. -
int Project::insert() { QSqlQuery query = DbConnector::getQuery(); if(!query.prepare("INSERT INTO project(name, description, color) VALUES(:n, :d, :c)")) { qDebug() << "PREPARE : " << query.lastError().text(); return 0; } query.bindValue(":n", name); query.bindValue(":d", description); query.bindValue(":c", color); if(!query.exec()) { qDebug() << query.lastError().number(); } return query.lastInsertId().toInt(); }
So it gaves me :
PREPARE : "no such table: project Unable to execute statement"
@mranger90 , thanks for your help.
I'll mark it as resolved. -
On a side note, support for named placeholder with SQLite requires a more recent version of Qt.
-
Can you explain a little more ?
-
Proper handling of named placeholder (e.g. the
:n
you are using) has been implemented recently and you are using an old 5.2.0 release current is 5.10.1 with 5.11 around the corner.