SqlLite Helper
HTML5 2012. 7. 6. 18:36
SqlLite Helper
HTML5에서 제공되는 WEB DB인 SqlLite Helper Module을 아래 코드와 같이 작성해(간단히;;) 보았습니다.
-
// SqlLite DB Helper
-
var SqlLite = new (function () {
-
-
var that = null;
-
var SqlLite = function () {
-
-
this.db = null;
-
return this;
-
}
-
-
SqlLite.fn = SqlLite.prototype = {
-
ready: function (name, callback, size, version, displayName) {
-
-
-
if (!name) return false;
-
-
callback = typeof callback === 'function' ? callback : function () { ; };
-
-
// options
-
size = size || 5 * (1024 * 1024);
-
version = version || '1.0';
-
displayName = displayName || 'sqlLiteDB';
-
-
var that = this;
-
if (window.openDatabase) {
-
this.db = window.openDatabase(String(name), version, displayName, size, function (db) {
-
callback.apply(that, Array.prototype.slice.call(arguments))
-
});
-
}
-
-
return this.db;
-
},
-
reader: function (sql, callback, errcallback) {
-
-
if (!sql) return false;
-
-
callback = typeof callback === 'function' ? callback : function () { ; };
-
errcallback = typeof errcallback === 'function' ? errcallback : function () { ; };
-
-
var that = this;
-
this.db.readTransaction(function (ts) {
-
ts.executeSql(sql, [], function (ts, ds) {
-
callback.call(that, ds);
-
delete ts;
-
}, function (e) {
-
errcallback.apply(that, [e, e.message]);
-
delete ts;
-
});
-
});
-
-
return this;
-
},
-
execute: function (sql, datas, callback, errcallback) {
-
-
if (!sql) return false;
-
-
callback = typeof callback === 'function' ? callback : function () { ; };
-
errcallback = typeof errcallback === 'function' ? errcallback : function () { ; };
-
-
var that = this;
-
this.db.transaction(function (ts) {
-
ts.executeSql(sql, datas, function (ts) {
-
callback.call(that);
-
delete ts;
-
}, function (e) {
-
errcallback.apply(that, [e, e.message]);
-
delete ts;
-
});
-
});
-
-
return this;
-
}
-
}
-
-
return SqlLite;
-
-
} ())();
SqlLite.ready(name, callback, size, version, displayName) 함수
name: DB명
callback: 완료 시 handler method
size: DB Size
version: DB Version
displayName: displayName
-
SqlLite.ready('testDB', function () {
-
-
var sql = [];
-
-
sql.push(' create table if not exists comments');
-
sql.push(' (');
-
sql.push(' idx integer primary key autoincrement,');
-
sql.push(' saboidx integer not null,');
-
sql.push(' menuidx integer not null,');
-
sql.push(' boardidx integer not null,');
-
sql.push(' usn text integer null,');
-
sql.push(' uid text not null,');
-
sql.push(' uc text not null,');
-
sql.push(' ucn text not null,');
-
sql.push(' up text not null,');
-
sql.push(' upn text not null,');
-
sql.push(' comments text not null,');
-
sql.push(' isdel integer not null,');
-
sql.push(' regdate text not null');
-
sql.push(' )');
-
-
this.execute(sql.join(''));
-
-
});
SqlLite.reader(sql, callback, errcallback) 함수
sql: Sql쿼리문
callback: 완료 시 handler method
errcallback: 에러 시 handler method
-
SqlLite.reader('select * from comments order by idx desc limit (' + pageSize + ') offset ' + pageSize + ' * ' + (page - 1),
-
function(ds){
-
-
for (var i = 0, length = ds.rows.length; i < length; i++) {
-
-
var idx = ds.rows.item(i).idx
-
, usn = ds.rows.item(i).usn
-
, uid = ds.rows.item(i).uid
-
, comments = ds.rows.item(i).comments
-
, regdate = ds.rows.item(i).regdate;
-
-
h.push('<tr>');
-
h.push('<td>' + idx + '</td>');
-
h.push('<td>' + usn + '</td>');
-
h.push('<td>' + uid + '</td>');
-
h.push('<td>' + comments + '</td>');
-
h.push('<td>' + regdate + '</td>');
-
h.push('<td><input type="button" onclick="return boardDelete(' + idx + ', ' + page + ')" value="삭제" /></td>');
-
h.push('</tr>');
-
}
-
-
elem.innerHTML = h.join('');
-
-
SqlLite.reader('select count(idx) as cnt from comments', function (ds) {
-
paging(page, ds.rows.item(0).cnt);
-
});
-
});
SqlLite.execute(sql, datas, callback, errcallback) 함수
sql: Sql쿼리문
datas: 파라메터 배열
callback: 완료 시 handler method
errcallback: 에러 시 handler method
-
SqlLite.execute('insert into comments (saboidx, menuidx, boardidx, usn, uid, uc, ucn, up, upn, comments, isdel, regdate) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
-
[SaboIdx, MenuIdx, BoardIdx, Usn, Uid, Uc, Ucn, Up, Upn, Comments, IsDel, Regdate],
-
function () {
-
boardList(pageSize);
-
});
참고 사이트:
http://www.w3.org/TR/webdatabase/#databases