Results 1 to 7 of 7

Thread: Bulk insert into SQLite

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Bulk insert into SQLite

    Hi,

    I have some text data in a CSV file which needs to be imported into a SQLite database. Following are the steps that can be used to import

    1. File->import.
    2. Select the CSV file that you want to import
    3. This data is now shown in a QTableWidget which can be edited.
    4. User can also select the column name which will correspond to some table in the database.
    5. When user is done with editing, he can click on the import button.

    Now, the problem.

    The SQLite database has 2 tables, let us call them name_table and address_table. As the name suggests name_table stores the names and the address_table stores the addresses. Also, these two are related by an id. name_table has a field called id and address_table has a field called nameid which will always be equal for a particular pair of name and address.

    While importing, I first insert into name_table, then get the id of the inserted row and then insert into address_table. I use QSqlTableModel to do this. Something like this:

    Qt Code:
    1. QSqlTableModel tableModel;
    2.  
    3. tableModel.setTable("name_table");
    4. tableModel.select();
    5. int rc = tableModel.rowCount();
    6. tableModel.insertRow(rc);
    7. rec = tableModel.record(rc);
    8.  
    9. rec.setValue(QString("prefix"),QVariant(nameDetails.prefix()));
    10. rec.setValue(QString("firstname"),QVariant(nameDetails.firstName()));
    11. rec.setValue(QString("middlename"),QVariant(nameDetails.middleName()));
    12. rec.setValue(QString("lastname"),QVariant(nameDetails.lastName()));
    13. rec.setValue(QString("suffix"),QVariant(nameDetails.suffix()));
    14. rec.setValue(QString("nickname"),QVariant(nameDetails.nickName()));
    15.  
    16. tableModel.setRecord(rc,rec);
    17. tableModel.submitAll();
    18. tableModel.select();
    19. rec = tableModel.record(tableModel.rowCount()-1);
    20. currentNameId = rec.value("id").toInt();
    21.  
    22. //using currentNameId now insert into address_table in similar way
    To copy to clipboard, switch view to plain text mode 

    I call the above code in a loop for every row in the table. This process is too slow and can insert not more than 100 records per minute. I know this is a completely wrong way but cannot think of anything better.

    Can someone please suggest the best way to do this so that the process of importing becomes fast ?

    Thanks a lot for your time.

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Bulk insert into SQLite

    Yesterday .... I imported a sqlite import file .sql from 36Mb on 3 minute moore as 250'000
    (500 Mb ram , 3GHz intel )
    row ... but... i not use qt sqlite plugin i use a static lib .... same on window / mac /linux
    http://svn.sourceforge.net/viewvc/qt..._sqlite_table/

    IMO comment http://sourceforge.net/forum/forum.p...orum_id=301849

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Bulk insert into SQLite

    Is there a Qt way to do this ?

    Thanks a lot

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Bulk insert into SQLite

    Quote Originally Posted by munna View Post
    Is there a Qt way to do this ?

    Thanks a lot
    Oh.. yes... (I can write only qt4 i owerflow heavy c++ books ;-) ) you import the the cvs file and convert to SQL - insert to sqlite and on the table model display only 100 row or 50 row ... button next >> prev <<< edit ready...

    Or other elegant method ..... only by window ...
    1- you make a new odbc connection to file cvs connect (setup file name path ecc.) all on plugin qt odbc ....

    a small snip to qt4 next & prev button have a look
    http://qtforum.de/forum/viewtopic.php?t=2431 simple & fast

    You need ony a QTableWidget & 2 button (next & prev)

    Or a little moore work and You build a fast static lib

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

    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

  6. #6
    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: 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).

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.