【问题标题】:How to decide where the SQLite file is stored in QML LocalStorage如何决定 SQLite 文件在 QML LocalStorage 中的存储位置
【发布时间】:2017-05-24 21:44:04
【问题描述】:

我有这个代码:

import QtQuick 2.6
import QtQuick.Controls 2.1
import QtQuick.LocalStorage 2.0

ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")

property var db // the database of this application
property string dbIdentifier: '/Users/cedo/desktop/test/DatabaseApplicationDB.db'
property string dbVersion: '1.0'
property string dbDescription: 'DatabaseApplicationDB'
property int dbEstimatedSize: 1000000

Component.onCompleted: {
    db = LocalStorage.openDatabaseSync(dbIdentifier, dbVersion, dbDescription, dbEstimatedSize);

    db.transaction(function(tx) {
        var sql = "create table if not exists mytable(id integer)";
        tx.executeSql(sql);
    });

}
}

创建数据库时 dbIdentifier 有效,但是当我在桌面中搜索 db 文件时,该文件不存在。它在哪里?或者如何决定放在哪里?

【问题讨论】:

    标签: javascript c++ qt mobile qml


    【解决方案1】:

    根据openDatabaseSync()方法documentation,函数原型为:

    object openDatabaseSync(string name, string version, string description, int estimated_size, jsobject callback(db))
    

    name是数据库名称

    version是数据库版本

    描述是数据库显示名称

    estimated_size 是数据库的估计大小,以字节为单位

    callback是一个可选参数,如果数据库尚未创建,则调用该参数。

    所以没有文件名参数。同样,根据documentations:

    这些数据库是特定于用户和特定于 QML 的,但可供所有 QML 应用程序访问。 它们存储在 QQmlEngine::offlineStoragePath() 的 Databases 子目录中,目前为 SQLite 数据库。

    为了能够将您的数据库文件放入某个自定义目录,您应该使用 QQmlEngine 或 QQmlApplicationEngine 的 setOfflineStoragePath(const QString &dir) 函数strong> 实例。

    要查看您的数据库文件当前所在的位置,请使用以下 C++ 代码:

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    qDebug() << engine.offlineStoragePath();
    

    更多信息here 和 here

    【讨论】:

      【解决方案2】:

      在 Linux 上是.local/share/*project_name*/QML/OfflineStorage/Databases

      【讨论】:

        猜你喜欢
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        • 1970-01-01
        • 2021-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多