- Home /
How to acces to an Sqlite database in android ?
Hi, i'm trying to create a game for android, and i created a database with sqlite in Assets/StreamingAssets, the idea is to store the base locally to read and write in it, I managed to make it work in the editor on pc but when i build for android i don't know how to acces this Db since every file are in jar format, and every exemple i found use WWW (to get bytes and create another file) but it's obsolete and i didn't find out how to make UnityWebRequest work, Can anyone help me ?
Thank you very much
Ps : i have already edit the build configuration to have the permission to access the SD card.
Answer by Larry-Dietz · Dec 07, 2019 at 03:10 PM
Take a look at this. I have used it many times in Android apps without problems..
Hope it helps, -Larry
Note that actual "magic" required for Andoid is this class DataService.cs. It actually tries to load the file from the persistent data path and if it doesn't exist yet it will use Unity's WWW class to read and extract the file from the strea$$anonymous$$g assets and store it in the persistent data path. The Strea$$anonymous$$g assets path is not a real file path and the data is read-only anyways. In order for SQLite to access the db file it has to be an actual file in the filesystem of android and not packed into the apk file.
Note that this script isn't really well made. First of all, even the WWW class still works, it's officially marked as obsolete. So while this most likely won't cause any issues in the near future, it may break in newer Unity versions. The second issue is the busy-waiting while loop on isDone. This is generally a bad approach. The loading / opening of the DB should be done in a coroutine maybe with a callback if necessary.
Answer by StephenA · Dec 07, 2019 at 11:06 PM
Thank for your response, i used it during my test but it didn't work like i expected to (with embeded objects), so i turn back to the "classic" sqlite library i found good tutorial on this both site : https://ornithoptergames.com/how-to-set-up-sqlite-for-unity/ and http://www.valentinourbano.com/Using-sqlite-in-Unity.html (MP me if the links are dead).
It now work on android but i still have to use WWW class that i don't know how to get ride off.