*SOLVED* Problem with Android SQLite in APK, dont run in Android device
Hi! The problem that i have is easy. I run my game in PC and all go well but when i build the project and run the apk in android device the game dont find the database.
I have tried many things and none works. Now i use this to acces the db and in pc works :S
connectionString = "URI=file:" + Application.dataPath + "/dataBase.db";
My db is in Assets/
I think the apk dont have the DataBase or i need to copy to the device i dont know... Please i need help, if you need more information tell me.
Thanks :)
I try this code and dont work :(
string filepath = Application.persistentDataPath + "/dataBase.db";
if(!File.Exists(filepath))
{
Debug.Log("Pasa");
// if it doesn't ->
// open Strea$$anonymous$$gAssets directory and load the db ->
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/dataBase.db"); // 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);
}
//open db connection
connectionString = "URI=file:" + filepath;
Answer by TruitiGames · Apr 17, 2017 at 09:06 PM
Hi! now all works! ^^ I have included new files and changed the code. I think all the problems with the DB in Android happend because we don't include some files that we need.
All files to work with SQLite in Android
And the code now is:
private string connectionString;
private string path;
void Start(){
ConnectionDB ();
CargarDatos();
}
public void ConnectionDB(){
if (Application.platform != RuntimePlatform.Android) {
connectionString = Application.dataPath + "/dataBase.db";
} else {
connectionString = Application.persistentDataPath + "/dataBase.db";
if(!File.Exists(connectionString)){
WWW load = new WWW ("jar:file://" + Application.dataPath + "!/assets/" + "dataBase.db");
while (!load.isDone){}
File.WriteAllBytes (connectionString, load.bytes);
}
}
}
private void CargarDatos(){
using (IDbConnection dbConnection = new SqliteConnection ("URI=file:" + connectionString)) {
dbConnection.Open ();
using (IDbCommand dbCmd = dbConnection.CreateCommand ()) {
string sqlQuery = String.Format ("SELECT * FROM Principal WHERE Id = 1"); //string sqlQuery = String.Format ("SELECT * FROM Misiones");
dbCmd.CommandText = sqlQuery;
using (IDataReader reader = dbCmd.ExecuteReader()){
while(reader.Read()){
totalMonedas = reader.GetInt32 (3);
totalGemas = reader.GetInt32 (2);
}
dbConnection.Close ();
reader.Close ();
}
}
textoGemas.text = totalGemas.ToString ();
textoMonedas.text = totalMonedas.ToString ();
}
}
I hope it helps ^^
Answer by LawyerOfGod · Jul 10, 2017 at 03:19 AM
Hola, sabes que tengo un problema muy similar al tuyo, estoy usando una base de datos con tablas, y en el editor de Unity funciona muy bien, pero cuando hago la APK, no carga la tabla... pareciera que da un loop mientras se conecta y se queda todo el tiempo asi.
He actualizado las libreria de las tablas y nada, que versión de Unity estas usando?.. Saludos @TruitiGames
Hola amigo tengo exactamente el mismo problema, lograste resolverlo? lei que habia que tener permisos de root en la aplicacion para generar cambios se supone en la base, pero incluso eh creado una y sigue sin ejecutar las consultas
Answer by Ace881 · Oct 13, 2017 at 12:09 PM
@TruitiGames hi, for FIle.Exists() need the apposite library?
Answer by unity_l3Ac7R_V3zAEkQ · Dec 30, 2018 at 06:21 AM
Hi, i am new one in unity. i need to connect Sqlite database in Android Device. in unity Editor is Working fine but Android device i am getting error ="Connection string format is invalid" ... here is my code. kindly help anyone
try {
if (Application.platform != RuntimePlatform.Android)
{
connection = Application.dataPath + "/StreamingAssets/Database.db";
if (!File.Exists(connection))
{
File.Create(connection);
}
connection = "URI=file:" + connection;
}
else
{
connection = Application.persistentDataPath + "/absdb.s3db";
if (!File.Exists(connection))
{
// if it doesn't ->
// open StreamingAssets directory and load the db ->
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/absdb.s3db"); // 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(connection, loadDB.bytes);
}
}
SqliteConnection con = new SqliteConnection(connection);
con.Open();
SqliteCommand CreateLifecmd= new SqliteCommand("CREATE TABLE Lifes( id INTEGER PRIMARY KEY AUTOINCREMENT,Lifes INTEGER not null); ",con);
CreateLifecmd.ExecuteNonQuery();
SqliteCommand CreateLevelscmd = new SqliteCommand("CREATE TABLE Levels( id INTEGER PRIMARY KEY AUTOINCREMENT,UnlockLevels INTEGER not null); ", con);
CreateLevelscmd.ExecuteNonQuery();
SqliteCommand insertLifecmd = new SqliteCommand("INSERT INTO Lifes (Lifes) Values (2)", con);
insertLifecmd.ExecuteNonQuery();
SqliteCommand InsertLevelscmd = new SqliteCommand("INSERT INTO Levels (UnlockLevels) Values (1)", con);
InsertLevelscmd.ExecuteNonQuery();
con.Open();
}
catch(Exception ex)
{
UiTExt.text = connection + "----" + ex.Message;
}
Your answer
Follow this Question
Related Questions
SQLLITE for android 0 Answers
Problem with body texture after build 0 Answers
How to override string resources of other android plugins in Unity 0 Answers
Android app crash when published on Play Store 3 Answers
After compiling and installing apk game becomes transparent and you can see the mobile home screen 1 Answer