3

I am using quite a lot of parameterized queries in my code for performance reasons. In short, some of them work, some don't.

I initialize the query during construction of my database wrapper like this:

QString querystring = QString("SELECT somevalue FROM sometable "
                      "WHERE one_feature = :one_feature AND other_feature = :other_feature ");
myquery = QSqlQuery(db);
myquery.setForwardOnly(true);
myquery.prepare(querystring);

myquery is a QSqlQuery member variable of my database wrapper. Later on, in the function that wants to use this query, I do something like

int db_wrapper::fetch_some_value (int arg1, int arg2) {
    myquery.clear();
    myquery.bindValue(":one_feature", arg1);
    myquery.bindValue(":other_feature", arg2);

    qDebug() << "Bound values: " << myquery.boundValues();

    bool OK = myquery.exec();
    if (!OK) {
        int number = myquery.lastError().number();
        qDebug() << "db error " << number;
        qDebug() << "db error " << myquery.lastError().text();

#ifdef USE_EXCEPTIONS
        throw "Could not fetch some_value!";
#endif
    }

    // process data...
}

I always get the same error message/output:

Bound values:  QMap((":one_feature", QVariant(int, 1) ) ( ":other_feature" ,  QVariant(int, 1) ) )  
db error  -1 
db error  " Parameter count mismatch" 
terminate called after throwing an instance of 'char const*'

The exception is not surprising, but the parameter count mismatch is. The call to boundValues actually shows the right values and all, still I get this error message. I have similar queries that work just fine.

I tried substituting positional bind values, renamed the placeholders, used ? and positional bind values, all to no avail. Does anyone have an idea what the problem might be?

I use Qt 4.7.3 and SQLite 3.7.4-2

3
  • Are the fields in the table declared as int ? Can you try with text (and pass quoted strings) ? Commented Sep 23, 2011 at 9:13
  • Yes, the fields are declared as "one_feature" integer NOT NULL REFERENCES "feature_table" ("id") in the generating SQL script. Changing the table is unfortunately not an option. I also tried quoting the parameters in the querystring, but that didn't work either. Commented Sep 23, 2011 at 9:28
  • 1
    Breadcrumbs for others: I had this error, but my issue was that I wasn't quoting the column names with the backtick ` and I had a column name that was also an sqlite keyword. Commented Nov 5, 2015 at 19:03

1 Answer 1

5

Usually this error means that the SELECT/UPDATE query itself is incorrect. You did not give the schema of the database so it's not possible to pinpoint which one. So one or more of somevalue, sometable, one_feature, or second_feature is not in the database/table.

Sign up to request clarification or add additional context in comments.

4 Comments

I cannot post the schema, sorry. However, I tried to execute the query with the parameters substituted with the values 1 and 1 (as in the example) in the SQLite database browser. This works fine.
How about the query.clear() call? Is it like you specify in the example? If you call it after prepare(), it would remove it from the query. Calling clear() is not usually required.
Actually, for some of the queries that threw this error, a typo on a field name was really to blame, so I accepted this answer. For others I still don't know, same as many people on the net. The problem is mostly blamed on Qt using ancient sqlite headers in its distribution.
Thanks @arne, it happened to be the problem also in my case. I had a query without placeholders which ran well in SQLiteStudio, but the QSqlQuery threw an error. It was a joined select and QSqlQuery needed all selected fields to be prefixed with their table name with a dot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.