Qt5:QML:LocalStorage

简介

LocalStorage是本地SQLite的数据库,程序关闭后仍存在,用于持久化数据。比如游戏数据的本地保存等等,在windows 10中位于(储存在哪不重要 搜一下OfflineStorage) 比如
C:\Users\用\AppData\Local\localstorage\QML\OfflineStorage\Databases

文件夹中

示例代码

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtQuick.LocalStorage 2.0


ApplicationWindow {
    visible: true
    width: 800
    height: 600
    title: qsTr("localstorage")

    property var localdb

    ListModel {
        id:idListMode
        ListElement {
            name: "testname"
            score: "99.5"
        }
    }


    ColumnLayout {
        id: columnLayout1
        anchors.fill: parent

        Rectangle {
            id: rectangle1
            Layout.fillWidth: true
            Layout.fillHeight: true
            height: 200
            color: "#ffffff"

            ListView
            {
                anchors.fill: parent
                model:idListMode
                delegate: Text {
                    text: name + "        :       " + score
                }

                Component.onCompleted:
                {
                    openDB();
                    getDBData();
                }
            }

        }

        Rectangle {
            id: rectangle2

            Layout.fillWidth: true
            height: 100


            Label {
                id: label1
                x: 91
                y: 35
                width: 101
                height: 21
                text: "姓名"
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                wrapMode: Text.WordWrap
            }

            Label {
                id: label2
                x: 91
                y: 62
                width: 101
                height: 21
                text: "成绩"
                wrapMode: Text.WordWrap
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
            }


            TextField {
                id: idName
                x: 205
                y: 36
                placeholderText: qsTr("")
            }

            TextField {
                id: idScore
                x: 205
                y: 63
                inputMask: "00"

            }

            Button {
                id: button1
                x: 416
                y: 35
                width: 112
                height: 48
                text:"添加"
                onClicked:
                {

                    if((idName.text.length>0)&&(idScore.text.length))
                    {
                        insertDBData(idName.text,idScore.text);
                        idListMode.append({"name":idName.text,"score":idScore.text});
                    }
                    else
                    {
                        console.log("cann't empty");
                    }
                }
            }
        }
    }

    //打开数据库,没有则创建,没有指定表则创建
    function  openDB()
    {
        localdb = LocalStorage.openDatabaseSync("QQmlValueRecordDB", "1.0", "QML LocalStorage", 1000000);

        localdb.transaction(function(tx){

            tx.executeSql('CREATE TABLE IF NOT EXISTS datasheet(name TEXT, score TEXT)');
        })
    }

    //数据库插入数据
    function insertDBData(name,score)
    {
        localdb.transaction(function(tx)
        {
            tx.executeSql('INSERT INTO datasheet VALUES(?,?)', [name,score]);
        })

    }

    //从数据库获取数据
    function getDBData()
    {
        localdb.transaction(function(tx)
        {
            var rs = tx.executeSql('SELECT * FROM datasheet');

            for(var i = 0; i < rs.rows.length; i++)
            {
                idListMode.append({"name":rs.rows.item(i).name,"score":rs.rows.item(i).score});
            }
        })

    }

}

这里写图片描述

源码下载

地址:github.com/CodeBees/qtExample/tree/master/localstorage

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值