U E D R , A S I H C RSS

html5/web Sql Database

1. ๊ฐœ์š”

  • SeeAlso) html5/web-storage
  • ๋กœ์ปฌ ์ €์žฅ์†Œ์˜ ์ผ๋ถ€. html5/web-storage์™ธ์— ์ถ”๊ฐ€ ์ œ๊ณต๋˜๋Š” ๊ฒƒ.
  • ๋ธŒ๋ผ์šฐ์ € ์ž์ฒด ๊ฒฝ๋Ÿ‰ DB
  • web storage์™€ ๋น„๊ต
    • WebStorage๋Š” ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์ ํ•ฉ.
    • WebSqlDatabase๋Š” ๋ณด๋‹ค ๊ตฌ์กฐ์ ์ด๊ณ  ์ฒด๊ณ„ํ™”๋œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ๋Œ€๋Ÿ‰์œผ๋กœ ์ €์žฅ
  • ์•ˆ์ •์ ์ธ ๊ฒฝ๋Ÿ‰ ๊ด€๊ณ„ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ ์ง€์›, ํ‘œ์ค€ SQL์งˆ์˜ ์ง€์›
  • SQLite๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ์žˆ๋‹ค.

1.1. ๋ธŒ๋ผ์šฐ์ € ์ง€์›

  • ์˜์™ธ๋กœ ํŒŒ์ด์–ดํญ์Šค์—์„œ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.
    • ์‹ฌ์ง€์–ด ๋‚ด๋ถ€ ๋””๋น„๊ฐ€ 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. ์‚ฌ์šฉ๋ฒ•

  • ๋น„๋™๊ธฐ ์‹์ด๋‹ค.
  • transaction์„ ์ง€์›ํ•œ๋‹ค.
    • transaction(), readTransaction

3.1. ๋ณ€์ˆ˜ ์„ ์–ธ

var html5rocks = {};
html5rocks.webdb = {};

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. hooking it all up

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 = '';
}

3.8. 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")');
});

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:41
Processing time 0.1009 sec