Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amrishodiq/cdcd91a5ee5dd984ddc1b424210c7a9e to your computer and use it in GitHub Desktop.
Save amrishodiq/cdcd91a5ee5dd984ddc1b424210c7a9e to your computer and use it in GitHub Desktop.
Create Database Tabel Only If Not Exists with JavaScript
// 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