Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>Chris</em> is right - use transactions; divide the data into chunks and then store it. </p> <p>"<em>... Unless already in a transaction, each SQL statement has a new transaction started for it. This is very expensive, since it requires reopening, writing to, and closing the journal file for each statement. This can be avoided by wrapping sequences of SQL statements with BEGIN TRANSACTION; and END TRANSACTION; statements. This speedup is also obtained for statements which don't alter the database.</em>" - Source: <a href="http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html" rel="noreferrer">http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html</a></p> <p>"<em>... there is another trick you can use to speed up SQLite: transactions. Whenever you have to do multiple database writes, put them inside a transaction. Instead of writing to (and locking) the file each and every time a write query is issued, the write will only happen once when the transaction completes.</em>" - Source: <a href="https://stackoverflow.com/questions/54998/how-scalable-is-sqlite/62220#62220">How Scalable is SQLite?</a></p> <pre><code>import csv, sqlite3, time def chunks(data, rows=10000): """ Divides the data into 10000 rows each """ for i in xrange(0, len(data), rows): yield data[i:i+rows] if __name__ == "__main__": t = time.time() conn = sqlite3.connect( "path/to/file.db" ) conn.text_factory = str #bugger 8-bit bytestrings cur = conn.cur() cur.execute('CREATE TABLE IF NOT EXISTS mytable (field2 VARCHAR, field4 VARCHAR)') csvData = csv.reader(open(filecsv.txt, "rb")) divData = chunks(csvData) # divide into 10000 rows each for chunk in divData: cur.execute('BEGIN TRANSACTION') for field1, field2, field3, field4, field5 in chunk: cur.execute('INSERT OR IGNORE INTO mytable (field2, field4) VALUES (?,?)', (field2, field4)) cur.execute('COMMIT') print "\n Time Taken: %.3f sec" % (time.time()-t) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload