PDA

View Full Version : QSqlQuery::bindValue() Question



kandalf
28th November 2006, 14:36
Hi guys, I'm having a problem with bindValue method. I have:


void SqliteHelper::insertData(const int &recordid, const int &controlid, const QString &value, const int &dataid)
{
QSqlQuery sqlQuery(this->sqlDb);
sqlQuery.prepare("INSERT INTO data (recordid, controlid, value, dataid) VALUES (:recordid,:controlid,:value,:dataid)");
sqlQuery.bindValue(":recordid", recordid);
sqlQuery.bindValue(":controlid", controlid);
sqlQuery.bindValue(":value", value);
sqlQuery.bindValue(":dataid", dataid);

if (sqlQuery.exec())
{
emit successExecute(sqlQuery.lastQuery());
}
else
{
emit errorExecute(sqlQuery.lastQuery(), sqlQuery.lastError().driverText());
}
}


The problem is that when query is executed lastError().driverText() returns "parameter count mismatch" I don't know what this error means.
Any ideas?

Thanx in advance.

jacek
28th November 2006, 17:13
What does lastError().type() return?

kandalf
28th November 2006, 18:46
What does lastError().type() return?
It returns 2 (QSqlError::StatementError)

jacek
28th November 2006, 20:53
It's quite weird. Does bindValue() work with other statements in your program?

sunil.thaha
29th November 2006, 06:55
To which database do you connect ?
Try using "?" in place of named parameters



sqlQuery.prepare( "INSERT INTO data (recordid, controlid, value, dataid) VALUES (?,?,?,?)" );
sqlQuery.bindValue(0, recordId ) ;
sqlQuery.bindValue(0, controlId ) ;
[...]

if (sqlQuery.exec())
emit successExecute(sqlQuery.lastQuery());
else
emit errorExecute(sqlQuery.lastQuery(), sqlQuery.lastError().driverText());

kandalf
29th November 2006, 15:45
Try using "?" in place of named parameters
I've already tried it, but it's solved now.
It was my problem. Somehow database file was corrupted, so, the "Parameter count mismatch" message was because the tables in database didn't have the right structure.

Thank you all again, and sorry for such a dummy post.

josbosmans
16th March 2007, 12:31
EDIT : I am an idiot :crying:

To help those other idiots who might make the same mistake :) :
if you originally use hardcoded queries, and then start using binding, don't forget to replace query.exec(querystring) by query.exec()

Took me a while to find out why I was getting the same error as the OP, even though the database was definately not corrupt.

qwertzui11
30th January 2010, 12:14
Got the same error when I was writing [...] prepare("INSTERT INTO [...]
Took me a while to find out that I've written INSERT wrong. Strange errorMsg ;)

Markus