*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 :slight_smile:

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

92237-captura.png

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 ^^

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

@TruitiGames hi, for FIle.Exists() need the apposite library?

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;
    }