Skip to content

Instantly share code, notes, and snippets.

@ptitfred
Created August 5, 2011 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptitfred/1128705 to your computer and use it in GitHub Desktop.
Save ptitfred/1128705 to your computer and use it in GitHub Desktop.
HTML5 DBStorage en CoffeeScript
# Handlers
nope = (tx, obj) ->
displayResult = (tx, rs) ->
alert "Succeeded!"
displayError = (tx, err) ->
alert "Error: #{err.message}"
class Storage
constructor: (@name, @displayName) ->
@db = openDatabase @name, "", @displayName, 1024*1024
createSchema: ->
@db.transaction (tx) ->
tx.executeSql "DROP TABLE OPTIONS", [], nope, displayError
tx.executeSql "CREATE TABLE OPTIONS (NAME TEXT PRIMARY KEY ASC, VALUE TEXT)", [], nope, displayError
insert: (name, value) ->
@db.transaction (tx) ->
tx.executeSql "INSERT INTO OPTIONS (NAME, VALUE) VALUES (?,?)", [name, value], nope, displayError
# Ouvre la base "coffee-db"
database = new Storage "coffee-db", "Coffee DB"
# initialise le schéma (au besoin)
database.createSchema()
# ajoute une ligne
database.insert "KEY1", "VALUE5"
database.insert "KEY2", "VALUE4"
database.insert "KEY3", "VALUE3"
database.insert "KEY4", "VALUE2"
database.insert "KEY5", "VALUE1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment