html webSQL
1 // webSQL 2 function createDbSuccess() { 3 console.log('创建数据库成功'); 4 } 5 //创建数据库 6 //数据库名称 版本号 描述文本 数据库大小 回调函数 7 var db = openDatabase('db27', '1.0', 'Test DB', 2 * 1024 * 1024,createDbSuccess()); 8 var msg; 9 var dbId=4; 10 var dbName='jack'; 11 db.transaction(function (tx) { 12 //创建表 13 tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)'); 14 //增 15 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "菜鸟教程")'); 16 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com")'); 17 tx.executeSql('INSERT INTO LOGS (id, log) VALUES (3, "jayson")'); 18 tx.executeSql('INSERT INTO LOGS (id,log) VALUES (?, ?)', [dbId, dbName]); 19 20 21 msg = '<p>数据表已创建,且插入了两条数据。</p>'; 22 // document.querySelector('#status').innerHTML = msg; 23 document.getElementById('status').innerHTML=msg; 24 }); 25 //删 26 //sql语句 参数变量 成功的回调 失败的回调 27 db.transaction(function(tx) { 28 tx.executeSql('DELETE FROM LOGS WHERE id=?', [4],function (tx,results) { 29 console.log('success'); 30 },function (tx,error) { 31 console.log('failed:'+error.message); 32 }); 33 }); 34 //改:更新 35 db.transaction(function(tx) { 36 tx.executeSql('UPDATE LOGS SET log=\'www.w3cschool.cc\' WHERE id=?', [2]); 37 }); 38 39 db.transaction(function (tx) { 40 //查 41 tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) { 42 var len = results.rows.length, i; 43 msg = "<p>查询记录条数: " + len + "</p>"; 44 // document.querySelector('#status').innerHTML += msg; 45 document.getElementById('status').innerHTML += msg; 46 for (i = 0; i < len; i++){ 47 msg = "<p><b>" + results.rows.item(i).log + "</b></p>"; 48 // document.querySelector('#status').innerHTML += msg; 49 document.getElementById('status').innerHTML += msg; 50 } 51 }, null); 52 });