- Home /
SqlLite , android and unity
Hello guys I reached a solution after hours and hours of research for getting data from a database to unity .It works really good on windows but not on android (i have a Plugins folder under Assets folder kindly find the attached file to see how the different Plugins were placed into the project ) and then i used the following code to read from database: on windows , it reached step 12 but on android it stops on step 6 so I guess it is going through a infinite while loop but what could cause this? Thanks for the help guys!
public void OpenDBtwo ()
{
debuggingthis.text = "step1";
Debug.Log("Call to open BD: GameDatabase.db" );
debuggingthis.text = "step2";
// check if file exists in Application.DataPath
string filepath = Application.dataPath + "/StreamingAssets/GameDatabase.db";
debuggingthis.text = "step3";
if (!File.Exists(filepath))
{
debuggingthis.text = "step14";
Debug.LogWarning("Arquive \"" + filepath + "\" doenst exist. tring to create from \"" + filepath);
// if it doesn't ->
// open StreamingAssets directory and load the db ->
debuggingthis.text = "step5";
WWW loadDB = new WWW("jar:file://" + filepath);
debuggingthis.text = "step6";
/*------reached this point on mobile-------*/
while (!loadDB.isDone) { }
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDB.bytes);
debuggingthis.text = "step7";
}
debuggingthis.text = "step8";
//open db connection
connection = "URI=file:" + filepath;
debuggingthis.text = "step9";
Debug.Log("Stabilishing connection to: " + connection);
debuggingthis.text = "step10";
dbConnection = new SqliteConnection(connection);
debuggingthis.text = "step11";
dbConnection.Open();
debuggingthis.text = "step12";
}
my guess was right ... i just tested it and it is an infinite while loop for I don't know what reason... please can anyone help me fix this and understand whats going on ?
$$anonymous$$e i made a different approach and work fine on editor, windows and android!
public DataService(string DatabaseName){
// check if file exists in Application.persistentDataPath
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, DatabaseName);
if (System.IO.File.Exists(filepath)){
_connection = new SQLiteConnection(filepath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
//Debug.Log("<color=#ffffff>Final PATH:</color> <color=#33bfea>" + filepath + "</color>");
} else {
#if UNITY_ANDROID
var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName); // this is the path to your Strea$$anonymous$$gAssets in android
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);
#endif
#if UNITY_EDITOR || UNITY_EDITOR_64 || UNITY_EDITOR_WIN
var _loaddb = string.Format(@"Assets/Strea$$anonymous$$gAssets/{0}", DatabaseName);
var filepathTo = string.Format("{0}/{1}", Application.persistentDataPath,DatabaseName);
File.Copy(_loaddb,filepathTo,true);
#endif
_connection = new SQLiteConnection(filepath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
//Debug.Log("<color=#ffffff>Final PATH:</color> <color=#33bfea>" + filepath + "</color>");
}
}
Here it's for android!
var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName); // this is the path to your Strea$$anonymous$$gAssets in android
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);
_connection = new SQLiteConnection(filepath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
Answer by GilbertoBitt · Apr 24, 2017 at 03:44 PM
public DataService(string DatabaseName){
// check if file exists in Application.persistentDataPath
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, DatabaseName);
if (System.IO.File.Exists(filepath)){
_connection = new SQLiteConnection(filepath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
//Debug.Log("<color=#ffffff>Final PATH:</color> <color=#33bfea>" + filepath + "</color>");
} else {
#if UNITY_ANDROID
var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName); // this is the path to your StreamingAssets in android
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);
#endif
#if UNITY_EDITOR || UNITY_EDITOR_64 || UNITY_EDITOR_WIN
var _loaddb = string.Format(@"Assets/StreamingAssets/{0}", DatabaseName);
var filepathTo = string.Format("{0}/{1}", Application.persistentDataPath,DatabaseName);
File.Copy(_loaddb,filepathTo,true);
#endif
_connection = new SQLiteConnection(filepath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
//Debug.Log("<color=#ffffff>Final PATH:</color> <color=#33bfea>" + filepath + "</color>");
}
}