Results 1 to 15 of 15

Thread: Using bound values in insert gives "parameter count mismatch" error in SQLite

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Symbian S60
    Thanked 1 Time in 1 Post

    Default Re: Using bound values in insert gives "parameter count mismatch" error in SQLite

    I had a similar problem leading to "parameter count mismatch" error.
    I was trying to bind the table name like:
    Qt Code:
    1. q.prepare("SELECT * FROM :table");
    2. q.bind(":table", "myTable");
    3. q.exec();
    To copy to clipboard, switch view to plain text mode 

    This is not possible, one can only bind filed values.
    Could be useful for future readers.

  2. The following user says thank you to remy_david for this useful post:

    curtwagner1984 (13th May 2017)

  3. #2
    Join Date
    Apr 2017
    Posts
    2
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: Using bound values in insert gives "parameter count mismatch" error in SQLite

    Quote Originally Posted by remy_david View Post
    I had a similar problem leading to "parameter count mismatch" error.
    I was trying to bind the table name like:
    Qt Code:
    1. q.prepare("SELECT * FROM :table");
    2. q.bind(":table", "myTable");
    3. q.exec();
    To copy to clipboard, switch view to plain text mode 

    This is not possible, one can only bind filed values.
    Could be useful for future readers.
    Your post helped my figure out what I was doing wrong with the same problem.
    It's should be pointed out that it's also impossible to bind column names like so:

    Qt Code:
    1. query.prepare("INSERT INTO Mytable (?,?) VALUES (?,?))"
    2. query.addBindValue("columnOne");
    3. query.addBindValue("columnTwo");
    4. query.addBindValue("firstValue");
    5. query.addBindValue("secondValue");
    To copy to clipboard, switch view to plain text mode 

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Using bound values in insert gives "parameter count mismatch" error in SQLite

    It should be pointed out that it's also impossible to bind column names like so:
    That's why the method is named addBindValue(). It isn't a general purpose "substitute anything for anything" method, it is specifically for binding variable values to VALUES() placeholders in a query.

    If you need to specify things like table and column names at run time, then construct the prepare() statement as a QString:

    Qt Code:
    1. QString myTableName = "MyTable";
    2. QString column1Name = "columnOne";
    3. QString column2Name = "columnTwo";
    4. QString queryStr = QString( "INSERT INTO %1 (%2,%3) VALUES (?,?);" ).arg( myTableName ).arg( column1Name ).arg( column2Name );
    5.  
    6. QSqlQuery query;
    7. query.prepare( queryStr );
    8. query.addBindValue( "firstValue" );
    9. query.addBindValue( "secondValue" );
    10. query.exec();
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 16th May 2017 at 03:55.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Parameter count mismatch in create table statement
    By croscato in forum Qt Programming
    Replies: 5
    Last Post: 4th February 2011, 09:38
  2. Replies: 1
    Last Post: 7th April 2010, 21:46
  3. Replies: 3
    Last Post: 25th August 2009, 13:03
  4. "Treat wchar_t as Built-in Type" to "yes" link error
    By sungaoyong in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2008, 11:45
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

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
  •  
Qt is a trademark of The Qt Company.