


How does the web front end update the cache? Singleton mode encapsulates opendatabase
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





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.

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.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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

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

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...

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 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.
