- Home /
Sqlite database and Android
I'm using Unity3D (for the first time) to create an Android app (for the first time) so I don't know much about either lol. I'm programming in C#.
I have a sqlite database which, from my understanding, should work natively on Android. From my research, I need to copy it from a folder in Unity called "Assets/StreamingAssets" over to Application.persistentDataPath on the device.
So this should work:
WWW loadDB = new WWW ("jar:file://" + Application.dataPath + "!/assets/" + FILE_NAME);
while (!loadDB.isDone) {}
File.WriteAllBytes (Application.persistentDataPath + "/" + FILE_NAME, loadDB.bytes);
And then just open the SQLiteConnection and read the results (which works in Unity on my computer, but does not work on the Android device).
Does anyone have any working code snippets or know why this isn't working for me? Thanks :)
$$anonymous$$E TOo.. i have spend a week for this issue.... im tired!!! pls somebody help us....
I copy sqlite DBs from strea$$anonymous$$g assets to persistent data using code that looks similar to yours (except I generally do it in a coroutine, using yield return loadDB;
ins$$anonymous$$d of your while loop). Unless I'm missing something, your code looks sound enough to me (for Android).
So please provide more information. "It does not work" is rarely helpful!
In particular, does it fail to load the file, or does it load the file but fail to write it? (you could e.g check the value of loadDB.error
and the size of loadDB.bytes
before your Write
call)
Or... is the problem somewhere else? You've shown us only the code for copying the file. From your vague description of the problem, it could be that the file actually being copied fine and you're doing something wrong when trying to connect to the DB. You can use adb
to inspect the filesystem on the android device and copy the DB file back to your computer to check it.
If you find that the file is being copied correctly, I would suggest that you edit your question, adding the code you're using to connect to the DB.
And note that that code is Android-specific. So I don't think you're correct to say that it "works in Unity on my computer".
To simplify coding I generally use this for the strea$$anonymous$$gAssetsPath
public static string strea$$anonymous$$gAssetsPath
{
get
{
if (Application.platform == RuntimePlatform.Android)
return "jar:file://" + Application.dataPath + "!/assets/";
else
return "file://" + Application.strea$$anonymous$$gAssetsPath +"/";
}
}
With that, I can use this for your first line to make it work for any platform...
WWW loadDB = new WWW (strea$$anonymous$$gAssetsPath + FILE_NA$$anonymous$$E);
Finally, any errors or warnings in the log on your Android device? (again, you can use adb
, to look at the logcat)
hello @Bonfire-Boy ! Can you describe more about how to use SQLite on Unity and apply it on Android device? Please. Thanks
Answer by Hizak · Feb 02, 2018 at 07:38 PM
i have the same prbloem just right now, everytingh works well on pc but android dont do the querys
When you say it doesn't do the queries, are you saying that it does connect to the database?
Your answer
Follow this Question
Related Questions
Unity 2019 sqlite3 connection android 0 Answers
Multiple Cars not working 1 Answer
Unity 4 Android Game cannot access SQLite DB (C# Script) 1 Answer
Distribute terrain in zones 3 Answers