VonnCC
VonnCC

Reputation: 467

Database not loaded on Device, but works on Unity Editor

Good day I am having a problem accessing my database on my mobile android device. It seems that it doesn't read my database or I don't know my database is not included upon build. But here is the catch, my database works fine in unity editor. I can't seem to find the problem on my mobile device because I don't have access on it's console. BTW here is my code:

Reading database:

void readDatabase(){

     string conn = "URI=file:" + Application.dataPath + "/Database/AtlasDB.sqlite";

    string sqlQuery = "SELECT * FROM UserScore";

    using (SqliteConnection c = new SqliteConnection(conn))
        {
            c.Open();
            using (SqliteCommand cmd = new SqliteCommand(sqlQuery, c))
            {
                using (SqliteDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                        {
                        currHighScore = rdr.GetInt32(0);
                            //          string name = reader.GetString(1);
                        currHighDist = rdr.GetInt32(1);

                            Debug.Log( "High Score : " + currHighScore + ", High Distance: " + currHighDist);
                        }
                }
            }
        }

}

Updating database:

void highscoreChecker(){

string conn2 = "URI=file:" + Application.dataPath + "/Database/AtlasDB";
        string  sqlQuery2 = "UPDATE UserScore SET Highscore = '" + MoveSanji.scoreAccumulate + "'";

using (SqliteConnection c = new SqliteConnection(conn2))
        {
            c.Open();
            using (SqliteCommand cmd = new SqliteCommand(sqlQuery2, c))
            {
                if (MoveSanji.scoreAccumulate > currHighScore) {

                    cmd.ExecuteReader();
                }
            }
        }

string  sqlQuery3 = "UPDATE UserScore SET HighDistance = '" + Mathf.RoundToInt( MoveSanji.distanceRun )+ "'";

        using (SqliteConnection c = new SqliteConnection(conn2))
        {
            c.Open();
            using (SqliteCommand cmd = new SqliteCommand(sqlQuery3, c))
            {
                if (Mathf.RoundToInt( MoveSanji.distanceRun) > currHighDist) {

                    cmd.ExecuteReader();
                }
            }
        }

    }

Im using sqlite

Note: I made my database using SQLite Database Browser 2.0 b1 -- Database is located on my Database Folder in Assets Folder -- I have Mono.Data, Mono.Data.Sqlite, sqlite3.dll, sqlite3.def, System.Data.dll on my plugins folder

Upvotes: 2

Views: 3061

Answers (1)

rutter
rutter

Reputation: 11452

Unity's build process is pretty smart about identifying built-in asset types that are used in your scenes, but it doesn't recognize your database file as a dependency and so it's not included in the build.

You can include raw files by keeping them in a StreamingAssets folder.

The path for those files depends on your build's target platform:

//PC
path = Application.dataPath + "/StreamingAssets";

//iOS
path = Application.dataPath + "/Raw";

//Android
path = Application.dataPath + "/Raw";

One important note for Android builds, from the manual page linked above:

Note that on Android, the files are contained within a compressed .jar file (which is essentially the same format as standard zip-compressed files). This means that if you do not use Unity’s WWW class to retrieve the file then you will need to use additional software to see inside the .jar archive and obtain the file.

Unless your use case can handle a read-only DB file embedded in a .jar archive, it may be easier to copy that file to another location such as Application.persistentDataPath and open that.

Upvotes: 2

Related Questions