[SOLVED] Sqlite on android

Working sample:
In project.xml

<android target-sdk-version="16" />
<android permission="android.permission.WRITE_INTERNAL_STORAGE"/>

Sample:

import haxe.db.Sqlite;
import openfl.filesystem.File;

var path:String="data.db";
var dbPath:String=File.applicationStorageDirectory.nativePath+"/"+path;
Sqlite.open(dbPath);

Could you share the code that’s crashing?

My first guess would be that you’re using a file, which is available on windows but on android is bundled in the apk. Or that you try to write next to the executable, failing for the same reason.

In project.xml i add:

<haxelib name="sqlite" />
<android target-sdk-version="16" />
<android permission="android.permission.WRITE_EXTERNAL_STORAGE"/>
<android permission="android.permission.READ_EXTERNAL_STORAGE"/>

My sample code:

import haxe.db.Sqlite;

Sqlite.open("data.db");

Without Sqlite.open project compile and launch on android device. In windows Sqlite.open create file, mb on android i must create it before open?
Thanks

Try this Sqlite.open(lime.system.System.applicationStorageDirectory + "/data.db");

Doing just “data.db” will probably write next to the game in windows, and try do to the same in android, but it’s not possible.

Thanks, i also found that this fix my issue:
In project.xml

<android target-sdk-version="16" />
<android permission="android.permission.WRITE_INTERNAL_STORAGE"/>

Sample:

import haxe.db.Sqlite;
import openfl.filesystem.File;

var path:String="data.db";
var dbPath:String=File.applicationStorageDirectory.nativePath+"/"+path;
Sqlite.open(dbPath);

where you put the data.db in your project? inside assets forlder? alagatar

You no need to create data.db file, it will’b created during runtime.

if you have already one db and want to use that data, where is the place to put the dabaBase them?

You can put it as an asset, and then make it copy to the application storage directory if it’s not there.

I have my db in assets/img… in neko i found the file like this:

var path:String = “img/ChildName.db”;
connection = Sqlite.open (path);

works perfect…

But in android the apk crash…

already try:

var path:String = “img/ChildName.db”;
var dbPath:String = lime.system.System.applicationStorageDirectory + “/” + path;
connection = Sqlite.open (dbPath);

but it dont works

You need to use the Assets class to access an asset in a cross platform manner.
http://api.openfl.org/openfl/Assets.html

how this will work with sqlite? I always use Assets to music png atlas image ect… but sqlite? i need to write and read the sqlite… Assets dont have getSqlite… and getBytes? where im gonna to store that bytes to read and write… i think is more easy to find a way to read the correct path to android…

Asset has getText and getByte which can allow you to write its content in File.applicationStorageDirectory.nativePath+"/data.db" and then use that path to open the connection to the database.

is File.applicationStorageDirectory.nativePath from the “import openfl.filesystem.File” ?

Type not found : openfl.filesystem.File

i cant use…

Don’t know, copied the code from @alagatar’s post,

I actually had mentioned lime.system.System.applicationStorageDirectory before, checked the api and it’s in it, should work with this.

if you create from cero your db; lime.system.System.applicationStorageDirectory works perfect for the job but if you want to find a ready populate db it wont work, because i dont know where to put this db in the first place Dx

No I’m telling you to copy your existing db from the assets to the applicationStorageDirectory.
Something like sys.io.File.putContents(Assets.getText("db.sqlite"), System.applicationStorageDirectory + "/db.sqlite");
Then you’ll be able to use it through Sqlite.open(System.applicationStorageDirectory + "/db.sqlite");

Sage advice:   “don’t build it … supply it.”   Provide an empty database as an asset.

In that database, be sure to include some table which contains a “version number.”

Also … get right now(!) into the habit of aggressively checking the outcome of any operation that you do, so that your app can respond graciously to whatever happens instead of “crashing” ignonimously.

I try that but dont work, but now I found the problem, dont know why the sqlite/1,9,0/ndll/Android/libsqlite3-7.so was the name of the .so file when i install the haxelib install sqlite, but it was searching for libsqlite3-v7-so, so if you install the haxelib install sqlite now you just have to change that name and all gonna work fine…

Thanks for all the help, hope this help another users too

The haxelib sqlite library is old, but perhaps I can update it.

I believe SQLite support was merged into the Haxe core for Neko and C++ some time ago?