Hi!
I have spended several hours to make the Sqlite works on Android but I still can not. On Unity editor all works fine, but when I compile for Android, build the apk, install the apk, the aplication does not work. There is a value that should be returned from the database, however this is not occurs.
Any help is very apprecite.
This is the code that I have used for connection with DB.
private void Awake()
{
//dbFile = "URI= File:assets/database/wordsdb.sqlite";
GenerateConnectionString();
}
private void Connection()
{
//connection = new SqliteConnection(dbFile);
connection = new SqliteConnection(connectionString);
command = connection.CreateCommand();
connection.Open();
}
public void GenerateConnectionString()
{
string DatabaseName = "wordsdb.sqlite";
#if UNITY_EDITOR
string dbPath = Application.dataPath + "/database/" + DatabaseName;
Debug.Log("Compilado no unity. " + Application.dataPath);
#else
//check if file exists in Application.persistentDataPath
string filepath = Application.persistentDataPath + "/" + DatabaseName;
if (!File.Exists(filepath) || new System.IO.FileInfo(filepath).Length == 0)
{
// if it doesn't ->
// open StreamingAssets directory and load the db ->
#if UNITY_ANDROID
WWW loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName); // this is the path to your StreamingAssets in android
//WWW loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName);
while (!loadDb.isDone) { } // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDb.bytes);
Debug.Log("Compilado no android. " + Application.dataPath);
#elif UNITY_IOS
var loadDb = Application.dataPath + "/Raw/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#elif UNITY_WP8
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#elif UNITY_WINRT
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#endif
}
var dbPath = filepath;
#endif
connectionString = "URI=file:" + dbPath;
}
I am testing with Asus Zenfone 3.