Results 1 to 7 of 7

Thread: Bulk insert into SQLite

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1

    Default Re: Bulk insert into SQLite

    I know this comment is old, but is it possible to improve performance using transactions? From what I understand from the sqlite performance comparison document (yes very old, but probably vaguely applicable) under normal circumstances the db file is opened and closed each time an insert is applied unless it is encapsulated in a transaction.

    Below is some pseudo code of what I mean;
    Qt Code:
    1. SQL>> BEGIN TRANSACTION; // execute as QSqlQuery
    2.  
    3. csv = FileParser::open( "somefile.csv", CSV );
    4.  
    5. while( current_record = csv.nextRecord() ) {
    6. QSqlQuery q( "INSERT INTO table (firstname,lastname,etc) VALUES (:firstname,:lastname,:etc)" );
    7.  
    8. q.bindValue( ":firstname", current_record[0] );
    9. q.bindValue( ":lastname", current_record[1] );
    10.  
    11. ...
    12.  
    13. if( !q.exec() ) throw RecordException; // or however you want to handle an error
    14. }
    15.  
    16. close( csv );
    17.  
    18. SQL>> COMMIT
    To copy to clipboard, switch view to plain text mode 
    I think the alternative and possibly preferred method maybe using QVariantList's with a prepared statement and execBatch() for the insert.

    Once you've loaded the data into the db, then grab the table using QSqlTableModel. Note it's just an idea, haven't actually tested it for performance. Hence the mashed code.

    Regards,

    Nate
    Last edited by natbobc; 16th November 2007 at 00:55. Reason: reformatted to look better

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

    Default Re: Bulk insert into SQLite

    Quote Originally Posted by natbobc View Post
    I know this comment is old, but is it possible to improve performance using transactions?
    If SQLite docs say so, then it might work. This really depends on the DBMS and other databases might like different constructs.

    For tips on PostgreSQL read this: http://www.depesz.com/index.php/2007...t-as-possible/

    Qt Code:
    1. while( current_record = csv.nextRecord() ) {
    2. QSqlQuery q( "INSERT INTO table (firstname,lastname,etc) VALUES (:firstname,:lastname,:etc)" );
    3. ...
    4. q.bindValue( ":firstname", current_record[0] );
    5. ...
    To copy to clipboard, switch view to plain text mode 
    If you pass the query string to QSqlQuery constructor, it will execute it immediately --- you have to use prepare() if you have placeholders:
    Qt Code:
    1. q.prepare( "INSERT INTO table (firstname,lastname,etc) VALUES (:firstname,:lastname,:etc)" );
    2. while( current_record = csv.nextRecord() ) {
    3. ...
    4. q.bindValue( ":firstname", current_record[0] );
    5. ...
    To copy to clipboard, switch view to plain text mode 
    Also by moving call to prepare() out of the loop, you can save a bit of time (esp. in case of nasty queries).

  3. #3
    Join Date
    Jul 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 1 Time in 1 Post

    Default Re: Bulk insert into SQLite


Similar Threads

  1. [Qt4.1] How to insert an image inside a Form?
    By Gonzalez in forum Qt Tools
    Replies: 5
    Last Post: 23rd September 2008, 11:20
  2. Insert QCheckBox into QTableView
    By wind in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2006, 16:15
  3. how to insert an ' in a database
    By jh in forum General Programming
    Replies: 3
    Last Post: 17th August 2006, 02:47
  4. insert a widget into a group "on-the-fly"
    By soul_rebel in forum Qt Programming
    Replies: 4
    Last Post: 15th January 2006, 00:29

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.