ความคิดเห็น: บทความนี้จะแนะนำรายละเอียดเกี่ยวกับวิธีการใช้คำสั่ง SQL ในฐานข้อมูลเว็บ HTML5 เพื่อนที่จำเป็นต้องรู้สามารถอ้างถึงได้
// เมธอด opendatabase เปิดฐานข้อมูลที่มีอยู่ หากไม่มีอยู่ฐานข้อมูลจะถูกสร้างขึ้น พารามิเตอร์คือชื่อของฐานข้อมูลเวอร์ชันคำอธิบายฐานข้อมูลและขนาดข้อมูล var db = window.opendatabase (MyDatabase, 1.0, คำอธิบายฐานข้อมูลของฉัน, 20000);วิธีใช้คำสั่ง SQL ในฐานข้อมูล
dbname.transaction (ฟังก์ชั่น (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">
// เมธอด opendatabase เปิดฐานข้อมูลที่มีอยู่ หากไม่มีอยู่ฐานข้อมูลจะถูกสร้างขึ้น
var db = window.opendatabase ("myDatabase", "1.0", "คำอธิบายฐานข้อมูลของฉัน", 20000);
var d = วันที่ใหม่ ();
// สร้างตารางข้อมูล
var sql = "สร้างตาราง mytable (ข้อความ mytitle, timestamp จริง)";
db.transaction (ฟังก์ชั่น (tx) {
tx.executesql (SQL);
-
// แทรกข้อมูลลงในตารางข้อมูล
db.transaction (ฟังก์ชั่น (tx) {
tx.executesql ("แทรกลงใน mytable (mytitle, timestamp) ค่า (?,?)", ["วิทยาลัยซอฟต์แวร์ Huasou, มหาวิทยาลัยกวางโจว 3", d.tolocalestring ()], null, null);
-
// ลบตารางข้อมูล
//db.transaction(Function (tx) {
// tx.executesql ("Drop Table MyTable");
-
db.transaction (ฟังก์ชั่น (tx) {
tx.executesql ("เลือก * จาก mytable", [],
ฟังก์ชั่น (tx, ผลลัพธ์) {
สำหรับ (var i = 0; i <result.rows.length; i ++) {
document.write ('<h1>' +
result.rows.item (i) ['mytitle'] + "" +
result.rows.item (i) ['timestamp'] +
'</h1>');
-
-
การทำงาน () {
การแจ้งเตือน ("ข้อผิดพลาด");
-
-
</script>
</body>
</html>