gpt4 book ai didi

javascript - 如何确定 SQLite 文件在 QML LocalStorage 中的存储位置

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:03 33 4
gpt4 key购买 nike

我有这个代码:

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 文件时,该文件不存在。它在哪里?或者如何决定放在哪里?

最佳答案

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

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

name is the database name

version is the database version

description is the database display name

estimated_size is the database's estimated size, in bytes

callback is an optional parameter, which is invoked if the database has not yet been created.

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

These databases are user-specific and QML-specific, but accessible to all QML applications. They are stored in the Databases subdirectory of QQmlEngine::offlineStoragePath(), currently as SQLite databases.

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

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

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

更多信息 herehere

关于javascript - 如何确定 SQLite 文件在 QML LocalStorage 中的存储位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168962/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com