Created
June 27, 2013 05:59
-
-
Save yargevad/5874279 to your computer and use it in GitHub Desktop.
Code to migrate existing test data from WebSQL to PG-SQLitePlugin-iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onDeviceReady() { | |
db = window.openDatabase('testdata', '0.1', 'Test Data', 1048576); | |
db1 = window.sqlitePlugin.openDatabase({name: 'test_data'}); | |
migrateData(db, db1, 'table1', [ 'col1', 'col2', 'col3', 'col4']); | |
migrateData(db, db1, 'table2', [ 'col1', 'col2']); | |
} | |
function migrateData(fr, to, tname, cols) { | |
try { | |
console.log('migrating table ['+ tname +'] ('+ cols.join() +')'); | |
fr.transaction(function (tx) { | |
migrateReadSource(tx, tname, cols, to); | |
}, dbFail); | |
} catch (e) { | |
console.log(e.message); | |
} | |
} | |
function migrateReadSource(tx, name, cols, to) { | |
try { | |
var sql = 'SELECT '+ cols.join() +' FROM '+ name; | |
tx.executeSql(sql, [], function (tx, results) { | |
migrateDest(name, cols, results, to); | |
}, dbFail); | |
} catch (e) { | |
console.log(e.message); | |
} | |
} | |
function migrateDest(name, cols, results, to) { | |
to.transaction(function (tx) { | |
migrateWriteDest(tx, name, cols, results); | |
}, dbFail); | |
} | |
function migrateWriteDest(tx, name, cols, results) { | |
try { | |
var qs = [], args = []; | |
for (var i = 0; i < cols.length; i++) { | |
qs[i] = '?'; | |
} | |
var sql = 'INSERT INTO '+ name +' ('+ cols.join() +') VALUES ('+ qs.join() +')'; | |
console.log(sql); | |
var i = 0; | |
for (i = 0; i < results.rows.length; i++) { | |
// transform rowset object to ordered execution parameters | |
for (var j = 0; j < cols.length; j++) { | |
args[j] = results.rows.item(i)[cols[j]]; | |
} | |
console.log(args.join()); | |
tx.executeSql(sql, args, null, dbFail); | |
} | |
console.log("migrated table ["+ name +"] ("+ i +" rows)"); | |
} catch (e) { | |
console.log(e.message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment