Results 1 to 8 of 8

Thread: QSqlQuery::bindValue() Question

  1. #1
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlQuery::bindValue() Question

    Hi guys, I'm having a problem with bindValue method. I have:
    Qt Code:
    1. void SqliteHelper::insertData(const int &recordid, const int &controlid, const QString &value, const int &dataid)
    2. {
    3. QSqlQuery sqlQuery(this->sqlDb);
    4. sqlQuery.prepare("INSERT INTO data (recordid, controlid, value, dataid) VALUES (:recordid,:controlid,:value,:dataid)");
    5. sqlQuery.bindValue(":recordid", recordid);
    6. sqlQuery.bindValue(":controlid", controlid);
    7. sqlQuery.bindValue(":value", value);
    8. sqlQuery.bindValue(":dataid", dataid);
    9.  
    10. if (sqlQuery.exec())
    11. {
    12. emit successExecute(sqlQuery.lastQuery());
    13. }
    14. else
    15. {
    16. emit errorExecute(sqlQuery.lastQuery(), sqlQuery.lastError().driverText());
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Kandalf
    There's no place like ~

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::bindValue() Question

    What does lastError().type() return?

  3. #3
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::bindValue() Question

    Quote Originally Posted by jacek View Post
    What does lastError().type() return?
    It returns 2 (QSqlError::StatementError)
    Kandalf
    There's no place like ~

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::bindValue() Question

    It's quite weird. Does bindValue() work with other statements in your program?

  5. #5
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::bindValue() Question

    To which database do you connect ?
    Try using "?" in place of named parameters

    Qt Code:
    1. sqlQuery.prepare( "INSERT INTO data (recordid, controlid, value, dataid) VALUES (?,?,?,?)" );
    2. sqlQuery.bindValue(0, recordId ) ;
    3. sqlQuery.bindValue(0, controlId ) ;
    4. [...]
    5.  
    6. if (sqlQuery.exec())
    7. emit successExecute(sqlQuery.lastQuery());
    8. else
    9. emit errorExecute(sqlQuery.lastQuery(), sqlQuery.lastError().driverText());
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

  6. #6
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::bindValue() Question [SOLVED]

    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.
    Kandalf
    There's no place like ~

  7. #7
    Join Date
    Mar 2007
    Posts
    2
    Thanked 1 Time in 1 Post

    Default Re: QSqlQuery::bindValue() Question

    EDIT : I am an idiot

    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.
    Last edited by josbosmans; 16th March 2007 at 12:56. Reason: updated contents

  8. #8
    Join Date
    Jan 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQuery::bindValue() Question

    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

Similar Threads

  1. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38
  2. Beginner C++ question
    By masoroso in forum General Programming
    Replies: 2
    Last Post: 19th April 2006, 14:15
  3. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37
  4. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12:03
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2006, 23:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.