Welcome to the CHICKEN Scheme pasting service
Bulk insert for SQLite3: comments welcome pasted by Saeren on Mon Sep 10 23:36:34 2012
;;; Insert a list of records into an SQLite3 database (uses the sql-de-lite egg). ;;; db: string, filename of database ;;; sql-str: string, sql-query string ;;; records: list, where each element us a list containing record values (define (bulk-insert db sql-str records) (let* ((db (open-database db)) (insert-all (lambda () (for-each (lambda (r) (apply exec (cons (sql db sql-str) r))) records)))) (with-transaction db insert-all) (unless (database-closed? db) (close-database db))))
Updated bulk insert for SQLite3 pasted by Saeren on Fri Sep 14 01:08:15 2012
;;; Per zbigniew's comments on IRC, I have now wrapped the transaction in ;;; call-with-database, removing the calls to open-database and close-database, ;;; as well as the unnecessary call to database-closed? The procedure now also ;;; only makes a single call to sql to prepare the sql statement, which should ;;; improve performance. Comments welcome. ;;; Insert a list of records into an SQLite3 database. ;;; db-file: string, filename of database ;;; sql-str: string, sql-query string ;;; records: list, where each element is a list containing record values (define (bulk-insert db-file sql-str records) (call-with-database db-file (lambda (db) (let* ((stmt (sql db sql-str)) (insert (lambda () (for-each (lambda (rec) (apply exec stmt rec)) records)))) (with-transaction db insert)))))
note on bulk insert added by zbigniew the caffeinated on Fri Sep 14 17:16:10 2012
In fact with sql-de-lite using (sql) multiple times has little penalty; it's just a hash table lookup, as the egg caches prepared statements. So any speedup is probably dwarfed by database insert time.