Home  >  Article  >  Web Front-end  >  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

云罗郡主
云罗郡主forward
2018-10-17 14:29:162135browse

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>

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:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete