- 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를 지원하지 않습니다") }
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 = '';
}













