Skip to content

Instantly share code, notes, and snippets.

@romeoh
Created March 22, 2013 04:59
Show Gist options
  • Save romeoh/5219047 to your computer and use it in GitHub Desktop.
Save romeoh/5219047 to your computer and use it in GitHub Desktop.
web DB
var romeoh = {}
romeoh.webdb = {}
romeoh.webdb.db = null
romeoh.webdb.open = function() {
var dbSize = 5 * 1024 * 1024; // 5MB
romeoh.webdb.db = openDatabase("test", "1.0", "Todo manager", dbSize);
}
romeoh.webdb.onError = function(tx, e) {
console.log("There has been an error: " + e.message);
}
romeoh.webdb.onSuccess = function(tx, r) {
// re-render the data.
// loadTodoItems is defined in Step 4a
}
romeoh.webdb.createTable = function() {
var db = romeoh.webdb.db;
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS " +
"book(id INTEGER PRIMARY KEY, title varchar(255) not null, author varchar(255) not null)", []);
});
}
romeoh.webdb.addTodo = function(todoText) {
var books = ['닥치고 정치', '김어준', '달려라 정봉주', '정봉주', '운명(문재인의)', '문재인', '나는 꼼수다. 1', '김어준', '노무현의 따뜻한 경제학', '변양균', '중생이 아프면 부처도 아프다:서이독경', '명진']
var db = romeoh.webdb.db;
db.transaction(function(tx){
var addedOn = new Date();
for(var index = 0; index < books.length; index+=2){
tx.executeSql("INSERT INTO book(title, author) VALUES (?,?)",
[books[index], books[index+1]],
romeoh.webdb.onSuccess,
romeoh.webdb.onError);
}
});
}
romeoh.webdb.getAllTodoItems = function(renderFunc) {
var db = romeoh.webdb.db;
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM book", [], renderFunc, romeoh.webdb.onError);
});
}
romeoh.webdb.open();
romeoh.webdb.createTable();
romeoh.webdb.addTodo()
romeoh.webdb.getAllTodoItems(loadTodoItems);
function loadTodoItems(tx, rs) {
console.log(rs.rows.item(0));
end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment