Results 1 to 7 of 7

Thread: SQLite insert into table -> Parameter count mismatch

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    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
  •  
Qt is a trademark of The Qt Company.