我想在我的Android游戏中使用Sqllite数据库我正在使用Haxe - OpenFl进行开发,但是当我尝试查询数据库时应用程序仍然崩溃。如果这是不可能的或有任何其他方式来处理Android中的数据,请告诉我,因为json和共享对象在我的场景中无法使用。
我也在OpenFl社区发布了这个问题 - 但我认为它与Haxe然后是OpenFL更相关。
我在做什么:
使用DB Browser
创建数据库并将其保存到assets/data/db/data.db
然后当应用开始时,我正在将其复制到lime.system.System.applicationStorageDirectory
它会在applicationStorageDirectory
trace("Connected to database " +_conn.dbName );
除了连接到数据库的文本外,它没有在跟踪中显示任何内容。我的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();
}
}
}
答案 0 :(得分:1)
我小睡了,在梦中得到了解决方法:D
问题出在这里
_conn = Sqlite.open(targetPath+NEW);
修正:
_conn = Sqlite.open(targetPath);
因为数据库名称已经在路径中:P
var targetPath: String = lime.system.System.applicationStorageDirectory+ NEW;
这就是为什么总是睡8个小时,否则最终会像我一样