S
S
Staspost2020-04-30 14:28:12
Android
Staspost, 2020-04-30 14:28:12

How to correctly register a database call in Android?

The Android Unity project has a pre-populated SQLite database. The database called db.bytes is located in the project in the \Assets\StreamingAssets folder.
When testing on Unity, the base works fine. But when installing APK on a real device via Google Play, the database does not function.
At the same time, a database is created on the smartphone (only I don’t understand if it was created on the right path). She is not empty. The table with locale in it is. I found the base on SD card in \Android\data\ProjectName\files\db.bytes folder. Should it be here in the public domain, or somewhere else?
I did the copying of the database according to the sample found on the Internet (albeit somewhat outdated, as far as I understand) and my method looks like this:

private void startDbCheckAndCopy()
    {
        try
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                
                //#if UNITY_ANDROID && !UNITY_EDITOR
                path = Application.persistentDataPath + "/db.bytes";                
                if (!File.Exists(path))
                {
                    WWW load = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "db.bytes");
                    while (!load.isDone) { }
                    File.WriteAllBytes(path, load.bytes);                    
                }
                //#endif
            }
            else if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                path = Application.dataPath + "/StreamingAssets/" + "db.bytes";
            } 
        }
        catch (Exception ex)
        {
            Debug.Log("ошибка" + ex.ToString());
        }
    }

This method is called the very first in the start() method when the game is loaded (the script hangs on the canvas). Immediately after it, the following method is called, which accesses the database, but nothing is eventually loaded from the database:
private void checkStartNew()
    {
        try
        {            
            dbconnection = new SqliteConnection("URI=file:" + path);
            dbconnection.Open();
            if (dbconnection.State == ConnectionState.Open)
            {
                cmd = new SqliteCommand();
                cmd.Connection = dbconnection;
                cmd.CommandText = "SELECT start FROM pref WHERE id=1";
                r = cmd.ExecuteReader();
                start = int.Parse(r[0].ToString());                     
            }
            else
            {
                Debug.Log("Error connection");
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
        r.Close();
    }

Actually the question is, what am I doing wrong and how should I change the code so that the database on the device works? Maybe somehow change the path of the database location?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-05-01
@staspost

Try to connect through this package.
https://github.com/praeclarum/sqlite-net
Both apk versions have so

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question