Unity android build sqlite数据库大小为零字节

问题描述 投票:0回答:1

[我被困于在android设备中运行unity3D android构建,但它在编辑器中运行,但是当我创建一个android构建并尝试在我的android设备中运行时,我在logcat中遇到如下错误:no such file or directoryNo such table尝试通过代码访问数据库,当检查我的android文件浏览器时,我发现数据库大小为零,我猜这应该不是。我将数据库保存在StreamingAssets文件夹中,该文件夹不为空。这是我的C#代码来设置数据库:

#if !UNITY_EDITOR
        var filepath = string.Format("{0}/{1}", Application.persistentDataPath, DatabaseName);
        if (!File.Exists(filepath))
        {
            Utils.Log(LogType.Debug, "Database does not exist");
            string DatabaseName = Constants.Settings.dbName;

#if UNITY_ANDROID
        // open StreamingAssets directory and load the db ->
        var filepath = string.Format("{0}/{1}", Application.persistentDataPath, DatabaseName);
        var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName);
        while (!loadDb.isDone) { }
        File.WriteAllBytes(filepath, loadDb.bytes);
#endif
        }

即使我在logcat中也看不到上面的日志“数据库不存在”,似乎如果阻塞就永远不会进入数据库,但是文件在设备的persistentDataPath中已经存在了。

c# sqlite unity3d logcat
1个回答
0
投票

我相信您的问题是,由于no such file or directory,在您运行该文件的某个时间该文件不存在(可能是无法打开的ENOENT)。发生这种情况是因为数据库文件夹/目录不存在,并且文件复制失败。但是,如果尝试打开数据库,则会创建数据库文件夹,但将打开一个数据库,但它是空栏,sqlite_master表和android_metadata以及no such table

解决方法是

a)修改代码,以便在if块内检查父文件(数据库目录是否存在),以及是否在复制前不使用File的mkdirs方法。

b)卸载应用程序或b)清除应用程序的数据。

c)重新运行。

  • b)将删除看似空的数据库文件,这就是为什么未输入if块的原因。
© www.soinside.com 2019 - 2024. All rights reserved.