Installing SQLite on MacOS (and probably for Windows) (Unity 2021.1.16)

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.

  1. 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).

  1. 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();
        }
    }
}
2 Likes

Thanks for the help. Although you didn’t specify, it makes sense to use the Mono.Data.Sqlite.dll from ‘unityjit-win32’ and not ‘unity’ or ‘unityjit-macos’ given the binaries you recommended to downloaed are also for windows.

For anybody else finding this as this ranks high on Google - I wasn’t happy with Unity’s Mono.Data.Sqlite.dll performance, especially when trying to add 100k of entries at once it would take half a minute or more. Tried different solutions like batching etc., was very frustrating.

Instead added SQLite by following these simple steps - and this solution turned out also super fast as it uses your native libraries if present. Adding 100k entries now happened within a second :

https://github.com/praeclarum/sqlite-net/issues/1023#issuecomment-821950695