Diego Ezquerro Calvo Diego Ezquerro Calvo - 10 months ago 68
C# Question

issue accessing SQLite database from Unity3D on Android device

I added a SQLite database to my Unity3D project, all works fine in while I´m working on the Unity Editor, but when I test the game on an Android device the access to the database fails.
Has anyone had the same problem and can tell me how to fix it?

An example of the code:

using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.Data;

public class DBAccess : MonoBehaviour {

void Start () {

string connectionString = "URI=file:" +Application.dataPath + "/myDataBase"; //Path to database.
IDbConnection dbcon;
dbcon = (IDbConnection) new SqliteConnection(connectionString);

dbcon.Open(); //Open connection to the database.

IDbCommand dbcmd = dbcon.CreateCommand();

string sql = "SELECT firstname, lastname FROM addressbook";

dbcmd.CommandText = sql;

IDataReader reader = dbcmd.ExecuteReader();

while(reader.Read()) {
string FirstName = reader.GetString (0);
string LastName = reader.GetString (1);
Debug.Log (FirstName + LastName);

// clean up
reader = null;
dbcmd = null;
dbcon = null;

I read in other threads of this forum that other people have had trouble making the build, isn't my case, I could generate the apk.

I've tested it in two diferents devices with the same result, so I think that it's not a problem of the device.

I am working with MonoDevelop as IDE, the 5.0.1 version of Unity3D and Sqlite 3.8.9, and all this on a laptop with windows 8.1

Thanks in advance.

Answer Source

I finally got it working. The problem was, at least in my case, that Android is based on Linux architecture, so it don't work with .dll libraries, I must use a .so librerie instead. This does not happen with MonoDevelop's own libraries; I guess that makes a translation of these libraries in the build, but not with the external libraries like sqlite3.dll.

I put this .so file into /Assets/Plugins/Android and Unity automatically finds it; in the inspector I must select 'Android' in 'Select platforms for puglin', and 'AR Mv 7' in CPU. I hope this will be useful for others with the same problem.

Since .so library is not on the official website of SQLite, if someone needs it, send me a message and I'll send it by mail.