W3CAPI 实例工具箱
运行代码
源代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HTML5 Web SQL 数据库 删除数据</title> </head> <body> <p id="info"></p> </body> <script type="text/javascript"> if ( typeof openDatabase == "undefined" ) { document.getElementById("info").innerHTML = "您的浏览器不支持Web SQL"; } else { //支持Web SQL var db = null; try { db = openDatabase('w3capi', '1.0', 'Test DB', 2 * 1024 * 1024, function (newdb) { console.log(newdb); }); } catch (e) { db = openDatabase('w3capi', '', 'Test DB', 2 * 1024 * 1024, function (newdb) { console.log(newdb); }); } db.transaction(function (tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS user (id unique, name)',[],function(tran,res){ document.getElementById("info").innerHTML += "创建数据表后执行回掉函数,结果为"+res.rowsAffected+"<br/>"; },function(tran,error) { document.getElementById("info").innerHTML += "创建数据表错误回掉,错误为:"+error.message+"<br/>"; }); tx.executeSql('INSERT INTO user (id, name) VALUES (1, "小王")',[],function(tran,res){ document.getElementById("info").innerHTML += "插入数据后执行回掉函数,结果为"+res.rowsAffected+"<br/>"; },function(tran,error) { document.getElementById("info").innerHTML += "插入数据错误回掉,错误为:"+error.message+"<br/>"; }); tx.executeSql('INSERT INTO user (id, name) VALUES (2, "小李")',[],function(tran,res){ document.getElementById("info").innerHTML += "插入数据后执行回掉函数,结果为"+res.rowsAffected+"<br/>"; },function(tran,error) { document.getElementById("info").innerHTML += "插入数据错误回掉,错误为:"+error.message+"<br/>"; }); var id=3,name='小张'; tx.executeSql('INSERT INTO user (id,name) VALUES (?, ?)', [id, name],function(tran,res){ document.getElementById("info").innerHTML += "插入数据后执行回掉函数,结果为"+res.rowsAffected+"<br/>"; },function(tran,error) { document.getElementById("info").innerHTML += "插入数据错误回掉,错误为:"+error.message+"<br/>"; }); //直接指定删除条件 tx.executeSql('DELETE FROM user WHERE id=1 ', [], function (tran, res) { document.getElementById("info").innerHTML += "删除数据后执行回掉函数,结果为"+res.rowsAffected+"<br/>"; },function(tran,error) { document.getElementById("info").innerHTML += "删除数据错误回掉,错误为:"+error.message+"<br/>"; }); /* //删除所有记录 tx.executeSql('DELETE FROM user ', [], function (tran, res) { document.getElementById("info").innerHTML += "删除数据后执行回掉函数,结果为"+res.rowsAffected+"<br/>"; },function(tran,error) { document.getElementById("info").innerHTML += "删除数据错误回掉,错误为:"+error.message+"<br/>"; }); */ /* //打开注释使用 动态参数绑定指定删除条件 var id = 1; tx.executeSql('DELETE FROM user WHERE id=? ', [id], function (tran, res) { document.getElementById("info").innerHTML += "删除数据后执行回掉函数,结果为"+res.rowsAffected+"<br/>"; },function(tran,error) { document.getElementById("info").innerHTML += "删除数据错误回掉,错误为:"+error.message+"<br/>"; }); */ }); } </script> </html>
运行结果
友情提示
您正在使用的浏览器为IE浏览器,或者使用了IE模式
如果你使用的是IE浏览器,建议您下载
360急速浏览器
/
火狐浏览器
/
Opera浏览器