I have been trying to get sqlite to work on Android for quite a while now. The problem seems to be that Mono.Data.Sqlite.dll can not find sqlite3.dll . I am thinking, there could be multiple solutions.
Locate the missing sqlite3.dll and include it in the apk;
Use the native libsqlite.so which is included in Android;
Compile sqlite from source and use as a plugin.
However, each one is easier said then done. If you know the answer to any of my question, please don’t hesitate to reply. Anything helps. My questions are:
a. Can I use the win32 sqlite3.dll from sqlite.org, or do I need a sqlite3.dll assembly specific for Mono;
b. How can I include it in the apk? Where should it go?
a. Is this the job of Mono.Data.Sqlite.dll?
b. Is there another way to access the libsqlite.so which is already in Android?
a. Can I compile using Android NDK? Isn’t that specifically for porting C to Java, since it uses JNI?
b. If I can not use Android NDK, how should I do it?
c. How to wrap around the library using C#? I have indeed located a linux C# wrapper for sqlite, it import “kernel32” to handle pointers among other things. I do not know how to adapt it for Unity.
create a “plugin” folder (or whatever name) into your “Assets” folder.
put the latest sqlite3.dll from the web into it
copy paste the DLLs “Mono.Data.Sqlite.dll” and “System.Date.dll” from your Unity/Mono directory.
The 3rd step is very important not to mess with, you absolutely have to put the 2 dll from Unity/Mono directory, or they won’t recognize each other if one comes from another version (latest Mono for ex). I had horrendous unexplainable bugs before I realized this and replaced my files.
Bump, I just realized that like many other Android users, sqlite3.dll is not integrated into the Android build (but it works fine under Editor).
Any news on this problem ? I tried to include the dll in any possible directory, doesn’t change anything.
Well, a DLL is a Windows binary, so it’s obviously not going to run on Android (Linux based) unless it’s a Mono DLL (which will automatically be compiled by Unity), which I don’t believe it is.
Android itself has Sqlite support built into its APIs. I’m not sure how you’d interface with that using Unity, but it’s a much better approach than trying to get a native DLL to run on Android.
So, as long as sqlite appears to be supported by either iOS and Android natively, and because we can’t integrate it pseudo-natively into our Unity build, wouldn’t it be logical for Unity to create a native interface to it, just like OpenGL and visual Keyboards ?
In the end, I can’t find any answer to this question :
How can we possibly use Sqlite for Android, then ?
I tried without putting sqlite3.dll, which still makes it work in Editor (like it wouldn’t need sqlite3.dll), but the build crashes again with the same DllNotFoundException for sqlite3.dll. Which means it’s finally required.
It’s a bit lame, because it means that the Unity/Mono integrated Sqlite version cannot work at all.
Why integrating something that cannot work ?
Ouch, sorry no offense was intended, and I mean it when I say the magazine content looks very good It was just a matter of principle
Thanks for your advice, I’ll search on how to compile into .so
I’ve been searching over the web for a potential Shared Object Library of sqlite3, but can’t find any… even on www.sqlite.org ! (isn’t it odd ?)
Would there be any way to find a sqlite3.so anywhere ?
Still can’t get it to work, tried the SO in the Android folder under plugins, tried to rename it to sqlite.so, still get the DLL not found exception with the Android project with SQL Lite, not sure if I want to bother any more with this yet or not since I plan on writing something else using a simple binary file structure but this was going to be interesting if it worked.
FYI, this Shared Obejct has solved the DLLNotFoundException for me. Totally.
Another problem appeared, although not related to Sqlite, which was a NullReferenceException on .NET Cryptography functions. As databases are commonly used with cryptography, I put the solution here :
When you chose micro mscorlib Stripping level under building settings, it does strip what should not be stripped under Reflection classes of C#, as Unity point out here, under iOS hints (but not under Android, as it does pose a problem under Android too).
So I created the link.xml file, and now Cryptography perfectly works with my SQLite Databases.
Thanks again DTreble for the .SO, works like a charm. This should be provided natively in future Unity builds, imo.
I could solve the libraries on Android. But now there is like having access to the path of my database.
I try (C#)
String connection = “URI= file:jar:” + Application.dataPath + “!/assets/” + myDatabase.db;
dbcon = new SqliteConnection(connection);
the error is not finding the database. (logcat)
I/Unity ( 5599): SqliteException: Unable to open the database file
I/Unity ( 5599): at Mono.Data.Sqlite.SQLite3.Open (System.String strFilename, SQLiteOpenFlagsEnum flags, Int32 maxPoolSize, Boolean usePool) [0x00000] in :0
I/Unity ( 5599): at Mono.Data.Sqlite.SqliteConnection.Open () [0x00000] in :0
I/Unity ( 5599):
I/Unity ( 5599): (Filename: Line: -1)