Results 1 to 7 of 7

Thread: SQLite insert into table -> Parameter count mismatch

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default SQLite insert into table -> Parameter count mismatch

    Hi,

    I was following a tutorial to learn something about Qt and C++ but at one point my code does not work. I try to enter some data in a fresh created SQLite3 database. The database was create with the FireFox add-on and is empty at the moment. I double checked that all names are correct and watched the tutorial three time to figure out what I did wrong.
    Here is the code for inserting. I am quite sure that I am connected to the data base be cause I can read from a different table.
    Qt Code:
    1. Settings::Settings(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Settings)
    4. {
    5. ui->setupUi(this);
    6. MainWindow connect;
    7. if(connect.conOpen()){
    8. qDebug()<<("Connected...");
    9. }else{
    10. qDebug()<<("Not connected!");
    11. }
    12. }
    13.  
    14. void Settings::on_pushButton_ok_clicked(){
    15. // Save data to db
    16. MainWindow connect; // here are the functions conOpen() and conClose() defined
    17. QString str1, str2;
    18. QDateTime date(QDateTime::currentDateTime());
    19.  
    20. str1=ui->editName->text();
    21. str2=ui->editPassword->text();
    22.  
    23. QSqlQuery qry;
    24. qry.prepare("insert into table (name,password,date) values ('"+str1+"', "+str2+", "+date.toString()+")");
    25. if(qry.exec()){
    26. qDebug()<<("Data was saved");
    27. // Sent result to log on MainWindow
    28. connect.conClose();
    29. this->hide();
    30. }else{
    31. qDebug()<<(qry.lastError().text());
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    Before I try to write to the data base I closed the old connection and opened a new one. Here is the output:
    Qt Code:
    1. Connected to database...
    2. Connected...
    3. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    4. Connected to database...
    5. " Parameter count mismatch"
    To copy to clipboard, switch view to plain text mode 
    I have read that there are issues if you create a SQLite DB with Qt but I did not. Does anyone see what I did wrong?

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: SQLite insert into table -> Parameter count mismatch

    Quote Originally Posted by KeineAhnung View Post
    Qt Code:
    1. qry.prepare("insert into table (name,password,date) values ('"+str1+"', "+str2+", "+date.toString()+")");
    To copy to clipboard, switch view to plain text mode 
    First of all, the Sqlite documentation shows that table is a reserved keyword, so you should avoid using a table name of "table" as you show in your insert statement. Not sure if that's allowed (I didn't actually try it), but you should avoid using reserved keywords as object names or column names, etc.

    Secondly, it doesn't look to me like you have formatted the query values correctly. The name value is properly quoted in single quotes, but you're missing single quotes around the password and the date values which also need to be quoted unless they are numeric values.

    As a best practice, you should not build the SQL query like that, you should either use use parameterized queries as such:

    Qt Code:
    1. qry.prepare("insert into table (name,password,date) values (?,?,?)";
    2. qry.bindValue(0,str1);
    3. qry.bindValue(1,str2);
    4. qry.bindValue(2,date.toString());
    5. qry.exec();
    To copy to clipboard, switch view to plain text mode 
    or even better, use named query parameters:

    Qt Code:
    1. qry.prepare("insert into table (name,password,date) values (:name,:password,:date)");
    2. qry.bindValue(":name", str1);
    3. qry.bindValue(":password", str2);
    4. qry.bindValue(":date", date.toString());
    5. qry.exec();
    To copy to clipboard, switch view to plain text mode 
    Lastly, when you created the QSqlQuery qry on the stack, you used the default constructor, which uses an empty query string and a default QSqlDatabase() instance. You should have specified the DB instance in the constructor. This associates your query with the correct database instance (namely, the instance you already opened). Since you have not done that, this may also be the source of your "Parameter count mismatch" error as well. Here's how you should have specified the QSqlQuery declaration:

    Qt Code:
    1. QSqlQuery qry(db); // you don't show what your QSqlDatabase variable is named, so I used db as an example
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by KeineAhnung View Post
    I have read that there are issues if you create a SQLite DB with Qt but I did not. Does anyone see what I did wrong?
    There are no issues creating or using Sqlite databases with Qt. There is no need to close/reopen the database before you write to it.

    Good luck.

    Jeff

  3. The following user says thank you to jefftee for this useful post:

    KeineAhnung (12th June 2014)

  4. #3
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: SQLite insert into table -> Parameter count mismatch

    Hi Jeff,

    thanks for the hints! It were the single quotes and the missing constructor for the QSqlQuery.
    I should have seen the missing quotes. This is something I ran into a couple of times writing php code for MySQL. Actually this is why I wrote the sql statement like that. Parameterization looks odd to me. Is there an advantage doing this or is this just a personal style thing?

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite insert into table -> Parameter count mismatch

    But if you had used such a structure is probably this thread would have never been.

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: SQLite insert into table -> Parameter count mismatch

    Quote Originally Posted by KeineAhnung View Post
    Parameterization looks odd to me. Is there an advantage doing this or is this just a personal style thing?
    It has the advantage that you do not have to worry about quoting values, the query is easier to read and validate, and it is clear what values are inserted into the query. The first point has substantial security advantages in the face of user-input. Consider this example:
    Qt Code:
    1. QString qry = "UPDATE passwordTable SET password ='" + newPassword + "' WHERE userName = '" + userInput +"' ";
    To copy to clipboard, switch view to plain text mode 
    when the malicious user has provided this as userInput:
    Qt Code:
    1. dummy' OR 'x' = 'x
    To copy to clipboard, switch view to plain text mode 
    the query becomes:
    Qt Code:
    1. UPDATE passwordTable SET password ='opensesame' WHERE userName = 'dummy' OR 'x' = 'x'
    To copy to clipboard, switch view to plain text mode 
    which is obviously a bad thing. Using parameters avoids this possibility.

  7. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: SQLite insert into table -> Parameter count mismatch

    Quote Originally Posted by KeineAhnung View Post
    Parameterization looks odd to me. Is there an advantage doing this or is this just a personal style thing?
    There are two advantages to using parameterized queries:

    1. As written, your query string is susceptible to an SQL injection issue that could be exploited by someone.

    2. Improved performance. You won't notice a difference in your example, but if you were looping and inserting lots of rows with the same SQL statement using different data values, you should prepare the query outside of the loop (one time), then bind values and exec inside the loop. This allows the db engine to optimize the query when it is prepared and reduces overhead when executing the prepared query over and over again.

    As you have written your example, there's really no benefit to you doing a prepare/exec since you are building the query string dynamically and only executing the prepared query once. You could just have easily passed the query string to exec and skipped the prepare. I would recommend, however, that you get used to using parameterized queries, which are more secure and offer better performance.

    Good luck.

    Jeff

  8. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite insert into table -> Parameter count mismatch

    Quote Originally Posted by jthomps View Post
    There are two advantages to using parameterized queries:

    2. Improved performance. You won't notice a difference in your example, but if you were looping and inserting lots of rows with the same SQL statement using different data values, you should prepare the query outside of the loop (one time), then bind values and exec inside the loop. This allows the db engine to optimize the query when it is prepared and reduces overhead when executing the prepared query over and over again.
    If performance is important use preparing with ? char not names. This method is about 20-40% faster. This is my observation when the program copies the tens on millions of records from one database to another.

Similar Threads

  1. Replies: 14
    Last Post: 16th May 2017, 03:51
  2. Replies: 4
    Last Post: 1st February 2014, 21:13
  3. Insert unicode in SQlite
    By Kastagne in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2011, 14:07
  4. Sql problem - parameter mismatch count
    By Marina K. in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2011, 18:27
  5. Parameter count mismatch in create table statement
    By croscato in forum Qt Programming
    Replies: 5
    Last Post: 4th February 2011, 09:38

Tags for this Thread

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.