Home Web Front-end HTML Tutorial How does the web front end update the cache? Singleton mode encapsulates opendatabase

How does the web front end update the cache? Singleton mode encapsulates opendatabase

Oct 17, 2018 pm 02:29 PM

The content of this article is about how to update the cache on the web front-end? The singleton mode encapsulates opendatabase and has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

If you don’t understand the operation of the opendatabase database, it is recommended that you first study the previous article "Web Front-end Cache Four Opendatabase" and then look at the content of this article. The content of this chapter is also the last in the front-end cache series. A simple encapsulation of the opendatabase database. Of course, for those with Java background, you can ignore this article.

Okay, let’s not talk nonsense and go straight to the topic. Please read the text (still based on the example of the previous article):

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<!--
openDatabase与android里面的sqlite差不多
最好的选型存储
-->

<h1>opendatabse数据库操作</h1>

<button id="btn-create">创建user数据表</button>
<button id="btn-insert">插入数据</button>
<button id="btn-query">查询数据</button>
<button id="btn-update">修改数据</button>
<button id="btn-delete">删除数据</button>
<button id="btn-drop">删除user数据表</button>

<script type="text/javascript">

    let findId = id => document.getElementById(id);

    //模拟一条user数据
    let user = {
        username: "liuqiang",
        password: "123569874",
        info: "beaconApp开发团队中一员"
    };

    /**
     * 创建数据库 或者此数据库已经存在 那么就是打开数据库
     * name: 数据库名称
     * version: 版本号
     * displayName: 对数据库的描述
     * estimatedSize: 设置数据的大小
     * creationCallback: 回调函数(可省略)
     */
    let db = openDatabase("MySql", "1.0", "我的数据库描述", 1024 * 1024);
    let result = db ? "数据库创建成功" : "数据库创建失败";
    console.log(result);


    const USER_TABLE_SQL = "create table if not exists userTable (id integer primary key autoincrement,username varchar(12)," +
        "password varchar(16),info text)";

    const INSERT_USER_SQL = "insert into userTable (username, password,info) values(?,?,?)";

    const QUERY_USER_SQL = "select * from userTable";

    const UPDATE_USER_SQL = "update userTable set password = ? where username = ?";

    const DELETE_USER_SQL = "delete from userTable where username = ?";

    const DROP_USER_SQL = "drop table userTable";

    /**
     * 封装数据库工具类
     */
    class DatabaseUtil {
        /**
         * 构造方法
         * @param sqlSentence 数据库语句
         * @param params 数据库操作的参数
         */
        constructor(sqlSentence, ...params) {
            this.sqlSentence = sqlSentence;
            this.params = params;
        }

        //获取当前实例
        static getInstance(sqlSentence, ...params) {
            return new DatabaseUtil(sqlSentence, ...params)
        }

        /**
         * 执行数据库操作
         * @param callback  成功的回调
         * @param errorCallback 失败的回调
         */
        execute(callback, errorCallback) {
            db.transaction(tx => {
                tx.executeSql(this.sqlSentence, this.params, callback, errorCallback)
            })
        }
    }

    /**
     * 点击事件 增删查改
     */
    let btnCreate = findId("btn-create");
    let btnInsert = findId("btn-insert");
    let btnQuery = findId("btn-query");
    let btnUpdate = findId("btn-update");
    let btnDelete = findId("btn-delete");
    let btnDrop = findId("btn-drop");
    //创建数据表
    btnCreate.onclick = () => DatabaseUtil.getInstance(USER_TABLE_SQL)
        .execute(function (transaction, resultSet) {
            alert('创建user表成功:' + result);
        }, function (transaction, error) {
            alert('创建user表失败:' + error.message);
        });
    //插入数据
    btnInsert.onclick = () => DatabaseUtil.getInstance(INSERT_USER_SQL, user.username, user.password, user.info)
        .execute(function (transaction, resultSet) {
            alert("添加数据成功")
        }, function (transaction, error) {
            alert("添加数据失败:" + error.message)
        });
    //查询数据
    btnQuery.onclick = () => DatabaseUtil.getInstance(QUERY_USER_SQL)
        .execute(function (transaction, resultSet) {
            console.log(resultSet);
        }, function (transaction, error) {
            alert("查询失败:" + error.message)
        });
    //修改数据
    btnUpdate.onclick = () => {
        user.password = "111666666";//修改密码
        DatabaseUtil.getInstance(UPDATE_USER_SQL, user.password, user.username)
            .execute(function (transaction, resultSet) {
                alert("修改数据成功")
            }, function (transaction, error) {
                alert("修改数据失败:" + error.message)
            })
    };
    //删除数据
    btnDelete.onclick = () => DatabaseUtil.getInstance(DELETE_USER_SQL, user.username)
        .execute(function (transaction, resultSet) {
            alert("删除数据成功")
        }, function (transaction, error) {
            alert("删除数据失败:" + error.message)
        });
    //删除数据表
    btnDrop.onclick = () => DatabaseUtil.getInstance(DROP_USER_SQL)
        .execute(function (transaction, resultSet) {
            alert("删除数据表成功")
        }, function (transaction, error) {
            alert("删除数据表失败:" + error.message)
        });

</script>
</body>
</html>
Copy after login

The above is how to update the web front-end cache? The singleton mode encapsulates the full introduction of opendatabase. If you want to know more about HTML video tutorial, please pay attention to the PHP Chinese website.


The above is the detailed content of How does the web front end update the cache? Singleton mode encapsulates opendatabase. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles