Skip to content

Instantly share code, notes, and snippets.

@minhnc
Created March 16, 2012 03:51
Show Gist options
  • Save minhnc/2048439 to your computer and use it in GitHub Desktop.
Save minhnc/2048439 to your computer and use it in GitHub Desktop.
Batch Insert using Transaction
function batchInserts(numRecords, useTransaction) {
var db = Ti.Database.open('database.sql');
// Create Table
db.execute('DROP TABLE IF EXISTS test');
db.execute('CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT);');
var start = (new Date()).getTime();
if (useTransaction) {
db.execute('BEGIN');
}
// Insert data
for(var i = 0; i < numRecords; i++) {
db.execute('INSERT INTO test(id, name) VALUES (?, ?)', i, 'Item ' + i);
}
if (useTransaction) {
db.execute('COMMIT');
}
var end = (new Date()).getTime();
if (useTransaction) {
Ti.API.info('INSERT ' + numRecords + ' - USING TRANSACTION - Take: ' + (end - start) + ' ms');
} else {
Ti.API.info('INSERT ' + numRecords + ' - Take: ' + (end - start) + ' ms');
}
db.close();
}
batchInserts(5000, true);
batchInserts(5000, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment