function addHistory(time, action, details){
if(openDatabase != undefined)
db.transaction(
function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS History(time INTEGER,
action TEXT, details TEXT)',[]);// 创建日志记录表
tx.executeSql('INSERT INTO History VALUES(?, ?, ?)', [time,
action, details], // 插入一条日志
function(tx, rs) {
//alert("store: "+time+"-"+action+"-"+details);
},
function(tx, error) {
//alert(error.source + "::" + error.message);
});
});
}
清单的第一部分显示了如何调用日志记录,第二部分显示了日志记录的详细过程。在一个transaction中,首先判断表History是否存在,若不存在,则创建它。第二部分执行一条SQL语句,向数据库中插入当前的日志。
通过检索表History,我们可以查看系统日志,清单12显示了如何从数据库中查询系统日志,并将其显示出来。
清单 12.日志显示
-
var historyTable = document.getElementById("historyTable");
-
-
// 定义表头
-
historyTable.innerHTML = "";
-
var th = document.createElement('thead');
-
th.style = "color:#CC3300";
-
var th1 = document.createElement('td');
-
th1.align = "center";
-
th1.width=300;
-
th1.innerHTML = "Time";
-
var th2 = document.createElement('td');
-
th2.align = "center";
-
th2.width=100;
-
th2.innerHTML = "Action";
-
var th3 = document.createElement('td');
-
th3.align = "center";
-
th3.width=150;
-
th3.innerHTML = "Details";
-
th.appendChild(th1);
-
th.appendChild(th2);
-
th.appendChild(th3);
-
historyTable.appendChild(th);
-
-
if(openDatabase != undefined)
-
db.transaction(function(tx) {
-
tx.executeSql('SELECT * FROM History', [], function(tx, rs)
-
{
-
// 将日志逐条显示到表的各行中
-
for(var i = 0; i < rs.rows.length && i<15; i++) {
-
var tr = document.createElement('tr');
-
var td1 = document.createElement('td');
-
td1.style.paddingLeft = "3px";
-
td1.style.paddingRight = "3px";
-
-
var t = new Date();
-
t.setTime(rs.rows.item(i).time);
-
ttd1.innerHTML = t.toLocaleDateString()+
-
" "+t.toLocaleTimeString();
-
(责任编辑:12图资源库)