Home Web Front-end H5 Tutorial How to operate WebSQL database with H5

How to operate WebSQL database with H5

Mar 27, 2018 am 10:10 AM
html5 web

This time I will show you how to operate the WebSQL database in H5. What are the precautions for H5 to operate the WebSQL database? The following is a practical case, let's take a look.

HTML code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

<!DOCTYPE html>

<html>

    <head lang="en">

        <meta charset="UTF-8">

        <title>列车时刻表查询</title>

        <meta name="viewport" content="width=device-width,initial-scale=1">

        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />

        <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    </head>

    <script src="js/connect.js"></script>

    <body onload="init()">

        <p data-role="page" id="pageone">

            <p data-role="header" data-position="fixed">

                <h1>列车时刻表查询</h1>

            </p>

            <p data-role="main" class="ui-content">

                <p align="center">请给我留言</p>

                <table data-role="table" class="ui-responsive">

                    <thead>

                        <tr>

                            <th>姓名:</th>

                            <th>留言:</th>

                        </tr>

                    </thead>

                    <tbody>

                        <tr>

                            <td><input type="text" id="name"></td>

                            <td><input type="text" id="memo"></td>

                        </tr>

                    </tbody>

                </table>

                <button type="submit" onclick="saveData()">留言</button>

                <table data-role="table" data-mode="" class="ui-responsive" id="datatable">

                    <!--这里是留言板的显示区域-->

                </table>

            </p>

            <!--

                作者:ceet@vip.qq.com

                时间:2017-08-26

                描述:底部TAB

            -->

            <p data-role="footer" data-position="fixed">

                <p data-role="navbar">

                    <ul>

                        <li>

                            <a href="index.html#index" data-icon="grid" class="ui-btn-active">查询</a>

                        </li>

                        <li>

                            <a href="index.html#detail" data-rel="popup" data-icon="star">收藏</a>

                        </li>

                        <li>

                            <a href="test.html" data-icon="comment">给我留言</a>

                        </li>

                    </ul>

                </p>

            </p>

            <!--收藏功能-->

            <p data-role="popup" id="myPopup" class="ui-content" data-theme="b">

              <a href="#" data-rel="back" class="ui-btn ui-btn-a ui-corner-all ui-shadow ui-btn ui-icon-delete ui-btn-icon-notext ui-btn-right">Close</a>

              <p>收藏成功,暂且不做处理!.</p>

              <p>请点击右上角有个关闭按钮</p>

              <p><b>提示:</b> 你也可以点击弹窗的外部区域来关闭弹窗。</p>

            </p>

        </p>

    </body>

</html>

Copy after login

JS code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

/**

 * HTML5 操作本地WebSQL数据库

 * 作者:汪政

 * 时间:2017/08/26 15:03:19

 */

var datatable = null;

var db = openDatabase("MyData""""My Database", 1024 * 100);

//初始化函数方法

function init() {

    datatable = document.getElementById("datatable");

    showAllData();

}

//首先移除乱七八糟的东西

function removeAllData() {

    for(var i = datatable.childNodes.length - 1; i >= 0; i--) {

        datatable.removeChild(datatable.childNodes[i]);

    }

    var tr = document.createElement("tr");

    var th1 = document.createElement("th");

    var th2 = document.createElement("th");

    var th3 = document.createElement("th");

    th1.innerHTML = "姓名";

    th2.innerHTML = "留言";

    th3.innerHTML = "时间";

    tr.appendChild(th1);

    tr.appendChild(th2);

    tr.appendChild(th3);

    datatable.appendChild(tr);

}

//显示WebSQL中的数据

function showData(row) {

    var tr = document.createElement("tr");

    var td1 = document.createElement("td");

    td1.innerHTML = row.name;

    var td2 = document.createElement("td");

    td2.innerHTML = row.message;

    var td3 = document.createElement("td");

    var t = new Date();

    t.setTime(row.time);

    td3.innerHTML = t.toLocaleDateString() + " " + t.toLocaleTimeString();

    tr.appendChild(td1);

    tr.appendChild(td2);

    tr.appendChild(td3);

    datatable.appendChild(tr);

}

//显示所有的数据

function showAllData() {

    db.transaction(function(tx) {

        tx.executeSql("CREATE TABLE IF NOT EXISTS MsgData(name TEXT,message TEXT,time INTEGER)", []);

        tx.executeSql("SELECT * FROM MsgData", [], function(tx, rs) {

            removeAllData();

            for(var i = 0; i < rs.rows.length; i++) {

                showData(rs.rows.item(i))

            }

        })

    })

}

//添加数据

function addData(name, message, time) {

    db.transaction(function(tx) {

        tx.executeSql("INSERT INTO MsgData VALUES (?,?,?)", [name, message, time], function(tx, rs) {

                alert("留言成功!");

            },

            function(tx, error) {

                alert(error.source + "::" + error.message);

            }

    )

    })

}

//调用

function saveData() {

    var name = document.getElementById("name").value;

    var memo = document.getElementById("memo").value;

    var time = new Date().getTime();

    addData(name, memo, time);

    showAllData();

}

Copy after login
I believe you have mastered it after reading the case in this article Method, for more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use the prefix data-attribute and dataset

Drag event editor implements drag and drop upload Picture effect

The above is the detailed content of How to operate WebSQL database with H5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

Nested Table in HTML

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Table Border in HTML

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

HTML margin-left

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Moving Text in HTML

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

HTML Ordered List

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

HTML onclick Button

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

HTML Input Placeholder

See all articles