{{{
var html5rocks = {};
html5rocks.webdb = {};
}}
== 열기 ==
* 사용전에 반드시 데이터 베이스를 열어야 한다.
{{{
html5rocks.webdb.db = null;
html5rocks.webdb.open = function() {
var dbSize = 5 * 1024 * 1024; // 5MB
html5rocks.webdb.db = openDatabase('Todo', '1.0', 'todo manager', dbSize);
}
html5rocks.webdb.onError = function(tx, e) {
alert('Something unexpected happened: ' + e.message );
}
html5rocks.webdb.onSuccess = function(tx, r) {
// re-render all the data
html5rocks.webdb.getAllTodoItems(tx, r);
}
}}}
== 테이블 생성 ==
* {{{todo table. ID, todo, added_on}}}
{{{
html5rocks.webdb.createTable = function() {
html5rocks.webdb.db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS ' +
'todo(ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME)', []);
});
}
}}}
== 테이블에 데이터 추가 ==
* dynamic sql
{{{
html5rocks.webdb.addTodo = function(todoText) {
html5rocks.webdb.db.transaction(function(tx){
var addedOn = new Date();
tx.executeSql('INSERT INTO todo(todo, added_on) VALUES (?,?)',
[todoText, addedOn],
html5rocks.webdb.onSuccess,
html5rocks.webdb.onError);
});
}
}}}
== 검색 질의 ==
*
{{{
html5rocks.webdb.getAllTodoItems = function(renderFunc) {
html5rocks.webdb.db.transaction(function(tx) {
tx.executeSql('SELECT * FROM todo', [], renderFunc,
html5rocks.webdb.onError);
});
}
}}}
=== 결과 표시 ==
{{{
function loadTodoItems(tx, rs) {
var rowOutput = "";
for (var i=0; i < rs.rows.length; i++) {
rowOutput += renderTodo(rs.rows.item(i));
}
var todoItems = document.getElementById('todoItems');
todoItems.innerHTML = rowOutput;
}
function renderTodo(row) {
return '<li>' + row.ID +
'[<a onclick="html5rocks.webdb.deleteTodo(' + row.ID + ');"'>X</a>]</li>';
}
}}}
== 데이터 지우기 ==
{{{
html5rocks.webdb.deleteTodo = function(id) {
html5rocks.webdb.db.transaction(function(tx) {
tx.executeSql('DELETE FROM todo WHERE ID=?', [id],
loadTodoItems, html5rocks.webdb.onError);
});
}
}}}
== hooking it all up ==
=== init ===
{{{
function init() {
html5rocks.webdb.open();
html5rocks.webdb.createTable();
html5rocks.webdb.getAllTodoItems(loadTodoItems);
}
</script>
<body onload="init();">
}}}
=== adding ===
{{{
function addTodo() {
var todo = document.getElementById('todo');
html5rocks.webdb.addTodo(todo.value);
todo.value = '';
}
}}}
== transaction ==
{{{
var db = openDatabase('mydb', '1.0', 'my first database', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS foo (id unique, text)');
tx.executeSql('INSERT INTO foo (id, text) VALUES (1, "synergies")');
});
}}}
= 참고 =
* http://m.mkexdev.net/61
* http://www.html5rocks.com/tutorials/webdatabase/todo/
* http://html5doctor.com/introducing-web-sql-databases/
* http://dev.w3.org/html5/webdatabase/
* http://html5.jidolstar.com/tag/web%20sql%20databaseo