After spending a few hours perusing old videos and posts, I finally figured out how to install SQLite on my Mac version of Unity. I am using Unity version 2021.1.16. Not sure how this will work with other versions.
- You need to get the current version of SQLite itself.
-
Go to SQLite Download Page
-
Look under “Precompiled Binaries for Windows” (I know this sounds strange, but it works)
-
Download sqlite-dll-win64-x64-3360000.zip .
-
Extract the files. There should be 2 files:
-
sqlite3.def
-
sqlite3.dll
-
Copy these 2 files to Assets/Plugins in your Unity project. (Obviously, create Plugins folder if not there).
- You need to find this file: Mono.Data.Sqlite.dll
- Go to where your working version of the Unity application is installed. Mine happens to be in a directory called 2021.1.16f1, but it will of course depend on your actual version.
- Go into the directory Unity.app/Contents/MonoBleedingEdge/lib/mono/unityjit
- Find the file Mono.Data.Sqlite.dll in that directory.
- Copy the file to Assets/Plugins in your Unity project.
Magically, things should work now.
A small (and dumb) example of a program using SQLite to prove things are somewhat working:
using System.Data;
using UnityEngine;
using IDbCommand = System.Data.IDbCommand;
using IDbConnection = System.Data.IDbConnection;
using Mono.Data.Sqlite;
public class CreateTable : MonoBehaviour
{
string conn;
string sqlQuery;
IDbConnection dbconn;
IDbCommand dbcmd;
IDataReader dbreader; // not used in this example
string DATABASE_NAME = "/mydatabase.s3db";
void Start()
{
string filepath = Application.dataPath + DATABASE_NAME;
Debug.Log($"filepath={filepath}");
conn = "URI=file:" + filepath;
CreateATable();
}
private void CreateATable()
{
using (dbconn = new SqliteConnection(conn))
{
dbconn.Open();
dbcmd = dbconn.CreateCommand();
sqlQuery = "CREATE TABLE IF NOT EXISTS [my_table] (" +
"[id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
"[name] VARCHAR(255) NOT NULL," +
"[age] INTEGER DEFAULT '18' NOT NULL)";
dbcmd.CommandText = sqlQuery;
dbcmd.ExecuteScalar();
dbconn.Close();
}
}
}