Created
May 9, 2016 03:55
-
-
Save amrishodiq/cdcd91a5ee5dd984ddc1b424210c7a9e to your computer and use it in GitHub Desktop.
Create Database Tabel Only If Not Exists with JavaScript
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
// after this function is executed, it will return true/false to callback | |
// callback should be a function that require a boolean parameter | |
function isDataExists(callback) { | |
getDatabase().transaction(function(trx) { | |
worship.createTableIfNotExists(trx, function() { | |
var sql = "SELECT * FROM worshipPlaces"; | |
console.log("Executing "+sql); | |
trx.executeSql(sql, null, function(trx, results) { | |
console.log("Results: "+results.rows.length); | |
if (results.rows.length > 0) { | |
callback(true); | |
} else { | |
callback(false); | |
} | |
}); | |
}); | |
}); | |
} | |
// trx is transaction object as parameter from above function | |
// callback called only when SQL query is succeed | |
function createTableIfNotExists(trx, callback) { | |
var sql = "CREATE TABLE IF NOT EXISTS worshipPlaces (placemark_id unique, religion, name, address, lat, lng)"; | |
trx.executeSql(sql, null, | |
function(transaction, sqlResultSet) { | |
console.log("Table creation success"); | |
callback(); | |
}, | |
function(transaction, error) { | |
console.log("Error: "+error.message); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment