Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TruitiGames · Apr 14, 2017 at 03:30 PM · androidandroid buildsqlite

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TruitiGames · Apr 14, 2017 at 06:25 AM 0
Share

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;

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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

alt text

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


captura.png (6.0 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image avcjeewantha · May 07, 2019 at 06:28 AM 0
Share

How can I download above plugins files?

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hizak · Feb 02, 2018 at 07:46 PM 0
Share

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

avatar image
0

Answer by Ace881 · Oct 13, 2017 at 12:09 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges