Web SQL 的使用

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="content"></div>
    <script>
        // 返回数据库的实例
        var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024)
        // 实例上调用方法,接收一个回调函数
        db.transaction(function (tx) {
            //tx 是一个SQLTransaction对象,后面的所有操作都基于该对象。
            //创建表格
            tx.executeSql('CREATE TABLE IF NOT EXISTS STU(stuId unique,stuName)')
            //插入字段
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(111,222`)
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(${Math.random()},${Math.random()})`)
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(${Math.random()},${Math.random()})`)
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(${Math.random()},${Math.random()})`)
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(${Math.random()},${Math.random()})`)
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(${Math.random()},${Math.random()})`)
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(${Math.random()},${Math.random()})`)
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(${Math.random()},${Math.random()})`)
            tx.executeSql(`INSERT INTO STU(stuId,stuName) values(${Math.random()},${Math.random()})`)
        })
        // 查询
        db.transaction(function (tx) {
            tx.executeSql('SELECT * FROM STU', [], function (tx, result) {
                console.log(result.rows) //拿到数据
                msg = `<p>查询到的数据条数一共有:${result.rows.length}条</p>`
                content.innerHTML += msg
                for (var i = 0; i < result.rows.length; i++) {
                    msg = `<p><b>${result.rows.item(i).stuName}</b></p>`
                    content.innerHTML += msg
                }
            }, null)
        })

        // 删除数据
        db.transaction(function (tx) {
            tx.executeSql('DELETE FROM STU WHERE stuId=?', [111])
        })

        // 修改数据
        db.transaction(function (tx) {
            tx.executeSql('UPDATE STU SET stuName=? WHERE stuId=2', ["寒冰"])
        })
	 </script>
</body>

</html>
相关推荐
IT_陈寒2 小时前
Redis 7.0 实战:5个被低估但超实用的新特性,让你的QPS提升40%
前端·人工智能·后端
web守墓人2 小时前
【前端】ikun-pptx编辑器前瞻问题四:通过AI编写一个前端pptx编辑器
前端
泰勒疯狂展开2 小时前
Vue3研学-标签ref属性与TS接口泛型
前端·javascript·vue.js
小二·2 小时前
前端 DevOps 完全指南:从 Docker 容器化到 GitHub Actions 自动化部署(Vue 3 + Vite)
前端·docker·devops
忒可君2 小时前
2026新年第一篇:uni-app + AI = 3分钟实现数据大屏
前端·vue.js·uni-app
Komorebi゛2 小时前
【CSS】线性流动边框样式
前端·css·css3
我不吃饼干2 小时前
手写 Vue 模板编译(生成篇)
前端·vue.js
s小布丁2 小时前
vue2纯前端使用Docxtemplater生成word报告,包含echart图表,表格
前端
web小白成长日记10 小时前
企业级 Vue3 + Element Plus 主题定制架构:从“能用”到“好用”的进阶之路
前端·架构
APIshop11 小时前
Python 爬虫获取 item_get_web —— 淘宝商品 SKU、详情图、券后价全流程解析
前端·爬虫·python