Comment: This article will introduce in detail how to use SQL statements in HTML5 Web Database. Friends who need to know can refer to it.
//The openDatabase method opens the existing database. If it does not exist, a database will be created. The parameters are the name of the database, version, database description, and data size var db = window.openDatabase(mydatabase, 1.0, my database description, 20000);How to use SQL statements in database
dbname.transaction(function (tx) { tx.executeSql(sql); });
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
</style>
</head>
<body>
<div>
</div>
<script type="text/javascript">
//The openDatabase method opens the existing database. If it does not exist, a database will be created.
var db = window.openDatabase("mydatabase", "1.0", "My database description", 20000);
var d = new Date();
//Create a data table
var sql = "CREATE TABLE mytable (mytitle TEXT, timestamp REAL)";
db.transaction(function (tx) {
tx.executeSql(sql);
});
//Insert data into the data table
db.transaction(function (tx) {
tx.executeSql("INSERT INTO mytable (mytitle, timestamp) values(?, ?)", ["Huasou Software College, Guangzhou University 3", d.toLocaleString()], null, null);
});
//Delete the data table
//db.transaction(function (tx) {
// tx.executeSql("DROP TABLE mytable ");
//});
db.transaction(function (tx) {
tx.executeSql("SELECT * FROM mytable", [],
function (tx, result) {
for (var i = 0; i < result.rows.length; i++) {
document.write('<h1>' +
result.rows.item(i)['mytitle'] + " " +
result.rows.item(i)['timestamp'] +
'</h1>');
}
},
function () {
alert("error");
});
});
</script>
</body>
</html>