[SOLVED] Sqlite on android

I’m currently struggling with Sqlite on android, based on the info that I got here I managed to avoid some problems, but the main problem right now is that it creates a new database instead of reading from the one I provide in assets.

var dbpath: String = System.applicationStorageDirectory + "/" + "database.db";
var connection:Connection = Sqlite.open(dbpath);
var results: ResultSet = connection.request("SELECT * FROM high_scores ORDER BY score DESC LIMIT 10");

E/Exception( 3390): Error while executing SELECT * FROM high_scores ORDER BY score DESC LIMIT 10 (Sqlite error in SELECT * FROM high_scores ORDER BY score DESC LIMIT 10 : no such table: high_scores)

my database is in assets/data/database.db

how can I make the connection??
thanks in advance

The issue on Android is that assets are not extracted – they’re in the APK. If it is fast enough, perhaps you can read your database and write to a new location (like the database.db path you have in your code) if it does not exist.

var targetPath = System.applicationStorageDirectory + "/database.db";
if (!FileSystem.exists (targetPath)) {
    var bytes = Assets.getBytes ("assets/data/database.db");
    File.saveContent (targetPath, bytes);

is there anything else I need to do before I can use the code you sent? cause I added this

import haxe.io.Bytes;
import sys.io.File;
import lime.Assets;
import sys.FileSystem;

                var targetPath: String = System.applicationStorageDirectory+ "/" + "database.db";
		if (!FileSystem.exists (targetPath)) {
			var bytes: Bytes = Assets.getBytes (System.applicationStorageDirectory + "assets/data/database.db");
			var content: String = bytes.toString();
			File.saveContent (targetPath, content);

and I still get an error, it’s a different one so it’s a plus point :smiley:

[lime.utils.Assets] ERROR: There is no BINARY asset with an ID of “/data/data/com.Gamebook.haxe/files/assets/data/database.db”

usually it means that I have to add something in project.xml, but I’m not sure what exactly

Try Assets.getBytes ("assets/data/database.db"); (if that’s the right name for the file) instead of including System.applicationStorageDirectory

Assets uses asset ID names, not paths, to look things up, so assets/test.txt might resolve to C:\Path\To\Application\assets\test.txt on Windows, ~/path/to/application/assets/test.txt on Linux at runtime, etc :slight_smile:

1 Like

thanks, I actually had to remove the “assets/” as well,

this did the trick

var bytes: Bytes = Assets.getBytes ("data/database.db");

thank you :sunny:

1 Like