Skip to content

Instantly share code, notes, and snippets.

@kovrov
Created June 2, 2012 17:46
Show Gist options
  • Save kovrov/2859328 to your computer and use it in GitHub Desktop.
Save kovrov/2859328 to your computer and use it in GitHub Desktop.
Simple QML storage wrapper for settings
.pragma library
function Settings(applicationName, applicationVersion) {
this.db = openDatabaseSync(applicationName, applicationVersion)
this.db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS settings(name TEXT UNIQUE, value TEXT)')
})
}
Settings.prototype.set = function(name, value) {
this.db.transaction(function(tx) {
var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [name,value])
if (rs.rowsAffected === 0) {
throw { message: "should not happen" }
}
})
return value
}
Settings.prototype.get = function(name) {
var res
this.db.transaction(function(tx) {
var rs = tx.executeSql('SELECT value FROM settings WHERE name=?;', [name])
if (rs.rows.length > 0) {
res = rs.rows.item(0).value
}
})
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment