mercredi 30 septembre 2015

How to make Sqlite works for Android with Unity and SQLiteUnityKit

I'm working on a unity app, essentially for Android at the moment.

To make this app i need to use SQL and it looks like SQLite is well supported on every platform.

Despite this, i'm having trouble to make it work properly.

First here is what i used SQLiteUnityKit

It looks like a lot of people have been using this framework for building SQL app.

Here is the code i got to try and make it work from Doc

public class DatabaseManager : MonoBehaviour
{
SqliteDatabase sqlDB;

public Text Firstname;

void Awake() 
{
    string dbPath = System.IO.Path.Combine (Application.persistentDataPath, "game.db");
    var dbTemplatePath = System.IO.Path.Combine(Application.streamingAssetsPath, "default.db");

    if (!System.IO.File.Exists(dbPath)) {
        if (Application.platform == RuntimePlatform.Android)
        {
            WWW reader = new WWW(dbTemplatePath);
            while ( !reader.isDone) {}
            System.IO.File.WriteAllBytes(dbPath, reader.bytes);
        } else {
            System.IO.File.Copy(dbTemplatePath, dbPath, true);
        }
    }
    sqlDB = new SqliteDatabase(dbPath);
    //Insert
    string query = "insert into person values('B', 'David')";
    sqlDB.ExecuteNonQuery(query);
    query = "insert into person values('B', 'Alex')";
    sqlDB.ExecuteNonQuery(query);
    //Query
    var result = sqlDB.ExecuteQuery("SELECT * FROM person");
    var row = result.Rows[1];
    Debug.Log("lastname=" + (string)row["lastname"]);
    Debug.Log("firstname=" + (string)row["firstname"]);
    Firstname.text = (string)row ["firstname"];
}
}

The problem is that basically this code works fine when i launch the project as you can see:

enter image description here

But in my Android Emulator:

enter image description here

There is never a name coming. It just stays with "my lastname" which is nothing more than a placeholder..

They added the Android support and a lot of people have been using it so i guess i do something wrong but i can't figure out what it is..

Thank you guys for helping !

Aucun commentaire:

Enregistrer un commentaire