SQLLite Crashing in Android target


#1

Hi,

i want to use use Sqllite datbase in my Android Game i am developing, but the app keep crashing when i am trying to query the database. If this is not possible or have any other ways to deal with data in Android kindly let me know as the json and shared objects are not going to work with in my scenario.

What i am doing :

  • Making a database using DB Browser and saving it to the assets/data/db/data.db

  • then when app starts i am making a copy of it to the lime.system.System.applicationStorageDirectory

  • it creates the file in the applicationStorageDirectory

  • Then i try to connect to the newly created db file and it just connects but right after connecting to it, try to get the name of the db it is connected to
    trace("Connected to database " +_conn.dbName ); and it is not showing anything in the trace except the text connected to database.

  • Ignoring the name i tried to query the Database and it just closes my app without any error or anything i get to know what goes wrong.

My 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();
			
		}
	}

}

#2

Hmm, I haven’t tried the static approach before. Does your same code work on the desktop? Does it work without a static SQLite library?


#3

it do nothing when it is added or not i just added it when debugging.

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

i tried it on the windows but got this error:

i have MS Visual Studio 2015 community edition installed with Visual C++ and using NDK version r10e

[Trying to fix this ,but i don’t know if it helps me in the current flow ]

Update :
fixed the CL.exe error


#4

Is there any other approach to make it work ?


#5

Is it building for Windows now?

…or does it work on Neko?


#7

I took a nap and got the fix :joy: in my dreams:
The problem is here
_conn = Sqlite.open(targetPath+NEW);

Fix:
_conn = Sqlite.open(targetPath);

Because database name is already in the path :stuck_out_tongue:

I still didn’t check it yet, but will update this post.

That’s why always sleep for 8 hours otherwise will end up like me :smiley: