sqlite-net icon indicating copy to clipboard operation
sqlite-net copied to clipboard

Question: Unity Support

Open rdcm opened this issue 4 years ago • 8 comments

Hi!

Could the SQLite-net be used in Unity projects?

In the official description Unity not mentioned.

SQLite-net is an open source, minimal library to allow .NET, .NET Core, and Mono applications to store data in SQLite 3 databases. It was first designed to work with Xamarin.iOS, but has since grown up to work on all the platforms (Xamarin.*, .NET, UWP, Azure, etc.).

But I see a merged fix for IL2CPP: https://github.com/praeclarum/sqlite-net/pull/993

rdcm avatar Jan 11 '21 12:01 rdcm

@rdcm

diff --git a/src/SQLite.cs b/src/SQLite.cs
--- a/src/SQLite.cs	(date 1618732310971)
+++ b/src/SQLite.cs	(date 1618732310971)
@@ -4356,7 +4356,11 @@
 			Serialized = 3
 		}
 
+#if UNITY_ANDROID && !UNITY_EDITOR
+		const string LibraryPath = "sqliteX";
+#else
 		const string LibraryPath = "sqlite3";
+#endif
 
 #if !USE_CSHARP_SQLITE && !USE_WP8_NATIVE_SQLITE && !USE_SQLITEPCL_RAW
 		[DllImport(LibraryPath, EntryPoint = "sqlite3_threadsafe", CallingConvention=CallingConvention.Cdecl)]

shiena avatar Apr 18 '21 07:04 shiena

Hey @shiena, can you please explain, or send some information how SQLite works on Mac/iOS? Why the native library is not necessary?

gindemit avatar Nov 10 '21 21:11 gindemit

@khindemit Mac/iOS includes sqlite3 so it works without adding native libraries. On my macOS Catalina, /usr/lib/libsqlite3.dylib exists.

shiena avatar Nov 11 '21 05:11 shiena

Just to add a comment after doing some research - on macOS, if the library asked for in sqlite-net is "sqlite3" macOS will automatically look for a library with libsqlite3.dylib, which is included in the library lookup path by default. The lib prefix and .dylib are magic strings added to the lookup.

wiverson avatar Dec 05 '21 18:12 wiverson

Even more magic - I just checked my macOS Monterey 12.0.1 M1 install, and it's now /usr/lib/sqlite3 alone. Interesting.

wiverson avatar Dec 05 '21 18:12 wiverson

I forgot that DllImport should be __Internal in iOS. So the patch is as follows, along with the one for Android. https://docs.unity3d.com/2022.1/Documentation/Manual/NativePlugins.html

diff --git a/src/SQLite.cs b/src/SQLite.cs
index e5712f3..dc9e0d4 100644
--- a/src/SQLite.cs
+++ b/src/SQLite.cs
@@ -4468,7 +4468,13 @@ namespace SQLite
 			Serialized = 3
 		}
 
+#if UNITY_IOS && !UNITY_EDITOR
+		const string LibraryPath = "__Internal";
+#elif UNITY_ANDROID && !UNITY_EDITOR
+		const string LibraryPath = "sqliteX";
+#else
 		const string LibraryPath = "sqlite3";
+#endif
 
 #if !USE_CSHARP_SQLITE && !USE_WP8_NATIVE_SQLITE && !USE_SQLITEPCL_RAW
 		[DllImport(LibraryPath, EntryPoint = "sqlite3_threadsafe", CallingConvention=CallingConvention.Cdecl)]

shiena avatar Dec 23 '21 10:12 shiena

@shiena For iOS, If the intent is using the system-provided libsqlite3, LibraryPath should be kept as sqlite3. __Internal is only for linking extern methods with statically-linked native functions in Unity IL2CPP. Unity's documentation was not updated to include the fact that dynamic linking was supported since iOS 8. However, if the Unity project integrates SQLite using static library or source code, then LibraryPath should be kept as __Internal for all platforms, including Android.

heshuimu avatar Jan 09 '22 05:01 heshuimu

What about the other platforms Unity supports? Specifically:

  • Web
  • Linux
  • Consoles (Xbox, Playstation, Switch, et al)