Haxe - SQLLite 在 Android 目标中崩溃

Haxe - SQLLite Crashing in Android target

我想在我的 Android 我正在使用 Haxe - OpenFl 开发的游戏中使用 Sqllite 数据库,但是当我尝试查询数据库时应用程序一直崩溃。如果这不可能或有任何其他方法来处理 Android 中的数据,请告诉我,因为 json 和共享对象在我的场景中无法使用。

我也在 OpenFl 社区中发布了这个问题 - 但我认为它与 Haxe 的关系比 OpenFL 更相关。

OpenFl Community Post

我在做什么:

我的Project.xml

<android target-sdk-version="26" install-location="preferExternal" if="android" />
<android permission="android.permission.WRITE_EXTERNAL_STORAGE"/>
<android permission="android.permission.WRITE_INTERNAL_STORAGE"/>

<haxelib name="openfl" />
<haxelib name="hxcpp" />

<assets path="assets/data/db" rename="db" />

DBClass

package;
import haxe.io.Bytes;
import lime.Assets;
import openfl.system.System;
import sys.FileSystem;
import sys.db.Connection;
import sys.db.Sqlite;
import sys.io.File;

#if android
    // Make SQLite work on android by statically compiling the library
    import hxcpp.StaticSqlite;
#end

/**
 * ...
 * @author Sim
 */
class DBManager
{
    private var CLONE:String = "db/asset_database.db";
    private var NEW:String = "new_db.db";

    private var _conn:Connection = null;

    public function new()
    {
    }

    public function openDatabase():Void
    {
        trace("CREATING FILE");

        trace("targetPath: " +lime.system.System.applicationStorageDirectory);
        //trace("targetPath: " +lime.system.System.applicationDirectory); //Crashing the app
        trace("targetPath: " +lime.system.System.documentsDirectory);
        trace("targetPath: " +lime.system.System.desktopDirectory);

        var targetPath: String = lime.system.System.applicationStorageDirectory+ NEW;
        trace("targetPath " + targetPath);
        trace("FileSystem.exists(targetPath) " + FileSystem.exists(targetPath));

        //Debugging
        /*var bytes:Bytes = Assets.getBytes(CLONE);
        trace("bytes are here "+bytes);
        var content:String = bytes.toString();
        trace("content "+content);
        File.saveContent(targetPath, content);
        trace("Saved");*/

        //uncomment when done with errors
        /*if (FileSystem.exists(targetPath) == false)
        {
            var bytes:Bytes = Assets.getBytes(CLONE);
            var content:String = bytes.toString();
            File.saveContent(targetPath, content);
        }*/

        var bytes:Bytes = Assets.getBytes(CLONE);
        var content:String = bytes.toString();
        File.saveContent(targetPath, content);

        trace("Saved");

        try 
        {
            _conn = Sqlite.open(targetPath+NEW);
        }
        catch (e:Dynamic)
        {
            trace("Connection failed with error: "+e);
        }

        if (_conn != null)
        {
            trace("Connected to database " +_conn.dbName );

            //not getting any database name trying to query

            // and KaBoom app gone :D XD
            var result = _conn.request("SELECT * FROM TEST");
            trace("Query Result "+result.results());

            //if i comment then it will go and close the connection too
            //without breaking anything O_O             

            _conn.close();

        }
    }

}

我小睡了一下,在梦中得到了修复::D

问题就在这里

_conn = Sqlite.open(targetPath+NEW);

修复:

_conn = Sqlite.open(targetPath);

因为数据库名已经在路径中了:P

var targetPath: String = lime.system.System.applicationStorageDirectory+ NEW;

这就是为什么总是睡8个小时,否则会像我一样结束