HTML是超文本标记语言,是一种专门用来制作网页的语言,而HTML5就是它的第五个版本,2014年10月29日,万维网联盟宣布,经过接近8年的艰苦努力,该标准规范终于制定完成,该标准针对当前互联网的飞速发展制定了更加标准的网页语言规范及适用功能,同时也给我们开发者带来了更多的开发选择,学习它是每个前端开发者必修的课程。
if ( typeof openDatabase == "undefined" ) { alert("您的浏览器不支持Web SQL"); } else { //支持Web SQL }
var db = openDatabase('w3capi', '1.0', 'Test DB', 2 * 1024 * 1024,function (newdb){ console.log(newdb); });
db.transaction(function (tx) { });
db.transaction(function (tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS user (id unique, name,age)'); });
<script type="text/javascript"> if ( typeof openDatabase == "undefined" ) { document.getElementById("info").innerText = "您的浏览器不支持Web SQL"; } else { //支持Web SQL var db = openDatabase('w3capi', '1.0', 'Test DB', 2 * 1024 * 1024,function (newdb){ console.log(newdb); }); document.getElementById("info").innerText = "数据库 w3capi 已经创建成功了!"; } </script>执行一下
<script type="text/javascript"> if ( typeof openDatabase == "undefined" ) { document.getElementById("info").innerHTML = "您的浏览器不支持Web SQL"; } else { //支持Web SQL var db = openDatabase('w3capi', '1.0', '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/>"; }); //打开注释,使用executeSql函数的 第二个参数来实现动态绑定数据插入 /* 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/>"; }); */ }); } </script>执行一下
tx.executeSql('SELECT * FROM user ', [], function (tran, res) { var len = res.rows.length, i; var msg = "<p>查询user表中记录数量: " + len + "</p>"; document.querySelector('#info').innerHTML += msg; for (i = 0; i < len; i++){ msg = "<p><b>" + res.rows.item(i).name + "</b></p>"; document.querySelector('#info').innerHTML += msg; } },function(tran,error) { document.getElementById("info").innerHTML += "查询数据错误回掉,错误为:"+error.message+"<br/>"; });执行一下
interface SQLResultSet { insertId: number; rowsAffected: number; rows: SQLResultSetRowList; }
//直接指定删除条件 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('UPDATE user SET name=\'小王_new\' WHERE id=1 ', [], function (tran, res) { document.getElementById("info").innerHTML += "更新数据后执行回掉函数,结果为"+res.rowsAffected+"<br/>"; },function(tran,error) { document.getElementById("info").innerHTML += "更新数据错误回掉,错误为:"+error.message+"<br/>"; });执行一下