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.0220 sec