Answer the question
In order to leave comments, you need to log in
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());
}
}
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();
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question