HarmonyOS应用开发JSAPI-js数据库dataRdb 原创

鸿蒙时代
发布于 2022-11-18 15:46
浏览
1收藏

前置:
Api:8
语言:js开发

参考地址:
https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/js-apis-data-rdb-0000001380281470-V3

开始:
1.创建项目:
HarmonyOS应用开发JSAPI-js数据库dataRdb-鸿蒙开发者社区

2.示例代码
test.hml

<div class="container">
    <button type="capsule" class="btn" onclick="CreateRdbStore">创建数据库</button>
    <div class="dataRdpBtn">
        <button type="capsule" class="btn" onclick="insertData">写入数据</button>
        <button type="capsule" class="btn" onclick="selectData">查询数据</button>
        <button type="capsule" class="btn" onclick="updateData">修改数据</button>
        <button type="capsule" class="btn" onclick="deleteData">删除数据</button>
    </div>
    <div class="dataRdpBox">
        <text class="text"><span>写入姓名:{{ name }}</span></text>
        <text class="text"><span>写入年龄:{{ age }}</span></text>
        <divider style="background-color: black;height: 2px;"></divider>

        <text class="text"><span>查询姓名:{{ name1 }}</span></text>
        <text class="text"><span>查询年龄:{{ age1 }}</span></text>
        <divider style="background-color: black;height: 2px;"></divider>
    </div>
</div>

test.css

.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
}
.dataRdpBtn{
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 100%;
    flex-wrap: wrap;
}
.btn {
    font-size: 50px;
    margin: 10px;
}
.dataRdpBox{
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    width: 90%;
    height: 500px;
    border: 2fp;
}
.text{
    font-size: 50px;
    width: 100%;
    margin: 10px;
}

test.js


export default {
    data: {
        rdb: null,
        name: "",
        age: "",
        name1: "",
        age1: "",
    },
    //创建数据库
    CreateRdbStore() {
        var that = this;
        dataRdb.getRdbStore({
            name: "demoRdb.db",
        }, 1, function (err, rdbStoreData) {
            if (err) {
                console.log('获取失败' + err)
            } else {
                const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER)"
                rdbStoreData.executeSql(SQL_CREATE_TABLE, null)
                that.rdb = rdbStoreData;
                console.log('dataRdb success' + that.rdb)
            }
        })
    },

    //写入数据
    insertData() {
        let that = this;
        const dataObj = {
            "NAME": "张三",
            "AGE": 20,
        }
        let promise = that.rdb.insert("EMPLOYEE", dataObj)
        promise.then(async (ret) => {
            console.log("插入成功: " + ret)
            that.name = dataObj.NAME;
            that.age = dataObj.AGE;
        }).catch((err) => {
            console.info(err)
        })
    },

    //查询数据
    selectData() {
        let that = this;
        let selectData = new dataRdb.RdbPredicates("EMPLOYEE")
        selectData.equalTo("NAME", that.name)
        console.log("查询条件:" + that.name)
        that.rdb.query(selectData, ["ID", "NAME", "AGE"], function (err, resultSet) {
            console.info(" success resultSet columnNames:" + resultSet.columnNames)
            console.info(" success resultSet columnCount:" + resultSet.columnCount)
            if (resultSet.rowCount > 0) {
                resultSet.goToFirstRow();
                const id = resultSet.getLong(resultSet.getColumnIndex("ID"))
                const name = resultSet.getString(resultSet.getColumnIndex("NAME"))
                const age = resultSet.getLong(resultSet.getColumnIndex("AGE"))
                console.log("id:" + id + "||" + "name:" + name + "||" + "age:" + age);
                that.name1 = name;
                that.age1 = age;
            } else {
                console.log("NOT DATA");
            }
        })
    },

    //修改数据
    updateData() {
        var that = this;
        let updateValue = {
            "NAME": "李四",
            "AGE": 25,
        }
        let updateData = new dataRdb.RdbPredicates("EMPLOYEE")
        updateData.equalTo("NAME", that.name)
        console.log("查询条件:" + that.name)
        that.rdb.update(updateValue, updateData, function (err, ret) {
            console.log("修改数据成功:" + ret)
            that.name = updateValue.NAME;
            that.age = updateValue.AGE;
        })
    },

    //删除数据
    deleteData() {
        let that = this
        let deleteData = new dataRdb.RdbPredicates("EMPLOYEE")
        deleteData.equalTo("NAME", that.name)
        that.rdb.delete(deleteData, function (err, rows) {
            if (rows.rowCount > 0) {
                rows.goToFirstRow();
                const name = rows.getString(rows.getColumnIndex("NAME"))
                const age = rows.getLong(rows.getColumnIndex("AGE"))
                console.log("name:" + name + "||" + "age:" + age);
                that.name = "";
                that.age = "";
                that.name1 = "";
                that.age1 = "";
            } else {
                console.log("NOT DATA");
            }
            console.log("删除成功: " + rows)
        })
    }
}



3.效果如图:
HarmonyOS应用开发JSAPI-js数据库dataRdb-鸿蒙开发者社区

HarmonyOS应用开发JSAPI-js数据库dataRdb-鸿蒙开发者社区

4、代码地址
(https://gitee.com/jltfcloudcn/jump_to/tree/master/dataRdb)

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
js数据库dataRdb.docx 555.7K 27次下载
3
收藏 1
回复
举报
1条回复
按时间正序
/
按时间倒序
qq5cc1513933aef
qq5cc1513933aef

感谢分享

回复
2022-11-18 17:11:33
回复
    相关推荐