- html5
- html5/VA
- html5/canvas
- html5/drag-and-drop
- html5/form
- html5/geolocation
- html5/offline-web-application
- html5/others-api
- html5/outline
- html5/overview
- html5/richtext-edit
- html5/video&audio
- html5/web-storage
- html5/web-workers
- html5/webSqlDatabase
- html5/websocket
- html5/λ¬Έμ μ
- html5practice/roundRect
- html5practice/μ¦κ²¨μ°ΎκΈ°λͺ©λ‘λ§λ€κΈ°
1. κ°μ ¶
- SeeAlso) html5/web-storage
- λ‘컬 μ μ₯μμ μΌλΆ. html5/web-storageμΈμ μΆκ° μ 곡λλ κ².
- λΈλΌμ°μ μ체 κ²½λ DB
- web storageμ λΉκ΅
- μμ μ μΈ κ²½λ κ΄κ³ν μλ£κ΅¬μ‘° μ§μ, νμ€ SQLμ§μ μ§μ
- SQLiteλ₯Ό κΈ°λ°μΌλ‘ νκ³ μλ€.
1.1. λΈλΌμ°μ μ§μ ¶
- μμΈλ‘ νμ΄μ΄νμ€μμ μ§μνμ§ μλλ€.
- μ¬μ§μ΄ λ΄λΆ λλΉκ° sqlite μμλ λΆκ΅¬νκ³ .
- μ¬μ§μ΄ λ΄λΆ λλΉκ° sqlite μμλ λΆκ΅¬νκ³ .
- μλμ μ½λλ₯Ό μ΄μ©νλ©΄ μ§μ μ¬λΆλ₯Ό νμΈ ν μ μλ€.
if(!!window.openDatabase) { alert("νμ¬ λΈλΌμ°μ λ Web SQL Databaseλ₯Ό μ§μν©λλ€") } else{ alert("νμ¬ λΈλΌμ°μ λ Web SQL Databaseλ₯Ό μ§μνμ§ μμ΅λλ€") }
1.2. Indexed Database ¶
- SeeAlso) html5/indexedDatabase
- Indexed Databaseλ μλ‘ λ±μ₯ν λλ€λ₯Έ λ‘컬 μ μ₯μ μ€νμ΄λ€
- 2009λ
λ§κΉμ§ μ¬μ μ±
μ μ΄ μ§νμ€μ΄λ Web SQL Database μ λμμΌλ‘ νμνλ€
- νμ¬ Web SQL Database λ μ¬μ μ±
μ μ΄ μ€μ§λ μνμ΄λ©°, IndexedDB λΌλ μλ‘μ΄ μ€νμ΄
2. νΉμ§ ¶
- λΉλκΈ° μμ
μ΄λ―λ‘ UIμ μ΅μνμ μν₯μ μ€λ€.
- λλ©μΈ λ¨μλ‘ DBκ° λΆλ¦¬λλ€.
- νΉμ λλ©μΈμμ μμ±λ λλΉλ λ€λ₯Έ λλ©μΈμμ μ κ·Όν μ μλ€.
- νΉμ λλ©μΈμμ μμ±λ λλΉλ λ€λ₯Έ λλ©μΈμμ μ κ·Όν μ μλ€.
- μΌλ°μ μΈ DB μ¬μ©λ²κ³Ό λΉμ·νλ€. μ΄κ³ , μννκ³ , λ«κ³ .
3.2. μ΄κΈ° ¶
- μ¬μ©μ μ λ°λμ λ°μ΄ν° λ² μ΄μ€λ₯Ό μ΄μ΄μΌ νλ€.
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);
}
3.3. ν μ΄λΈ μμ± ¶
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)', []);
});
}
3.4. ν μ΄λΈμ λ°μ΄ν° μΆκ° ¶
- 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);
});
}
3.5. κ²μ μ§μ ¶
html5rocks.webdb.getAllTodoItems = function(renderFunc) {
html5rocks.webdb.db.transaction(function(tx) {
tx.executeSql('SELECT * FROM todo', [], renderFunc,
html5rocks.webdb.onError);
});
}
3.5.1. κ²°κ³Ό νμ ¶
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>';
}
3.6. λ°μ΄ν° μ§μ°κΈ° ¶
html5rocks.webdb.deleteTodo = function(id) {
html5rocks.webdb.db.transaction(function(tx) {
tx.executeSql('DELETE FROM todo WHERE ID=?', [id],
loadTodoItems, html5rocks.webdb.onError);
});
}
3.7.1. init ¶
function init() {
html5rocks.webdb.open();
html5rocks.webdb.createTable();
html5rocks.webdb.getAllTodoItems(loadTodoItems);
}
</script>
<body onload="init();">
3.7.2. adding ¶
function addTodo() {
var todo = document.getElementById('todo');
html5rocks.webdb.addTodo(todo.value);
todo.value = '';
}










