Created
December 22, 2015 07:50
-
-
Save fecub/4307458fb7b8eb9df279 to your computer and use it in GitHub Desktop.
LocalStorage with easy script
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 getDatabase() { | |
return LocalStorage.openDatabaseSync("AzanTime", "0.1", "VakitDatabase", 100); | |
} | |
function set(setting, value) { | |
var db = getDatabase(); | |
var res = ""; | |
db.transaction(function(tx) { | |
tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)'); | |
var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]); | |
if (rs.rowsAffected > 0) { | |
res = "OK"; | |
} else { | |
res = "Error"; | |
} | |
} | |
); | |
return res; | |
} | |
function get(setting, default_value) { | |
var db = getDatabase(); | |
var res=""; | |
try { | |
db.transaction(function(tx) { | |
var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]); | |
if (rs.rows.length > 0) { | |
res = rs.rows.item(0).value; | |
} else { | |
res = default_value; | |
} | |
}) | |
} catch (err) { | |
console.log("Database " + err); | |
res = default_value; | |
}; | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment