Here is a major update to the SQLite in Unity that support show to access an SQLite database in Unity using only Bolt.

For this to work you need to have SQLite setup in Unity already, and a database created inside your projects “Assets” folder.

This example connects to the database and asked for all of the “gamename” entries in the database table called “games”.

SELECT gamename FROM games

The data is returned to Bolt and accessed using A “Data Record Interface” that lets us put the returned values into a list.. (here we only process the result of 0 or first field of the array of returned results) resulting in a list of game names retrieved from the database.

Note: The DB_Results ‘Scene Variable’ that is storing all of the names of the games pulled from the database results is set to be a ‘List of String’.

If we changed the query to ask for the gamename and genre we would then need to process index “0” and “1” [0 = gamename & 1=genre] for each result.

SELECT gamename, genre FROM games
Click for a larger view of the graph.

There are a few extra “Assembly & Types” that need to be added to Bolt for this to work. Due to the project i was working on not all of these may be needed but you should be able to understand which ones are SQLite oriented.

Mono.Data.Sqlite, System.Data (For Sure)
Sqlite Command, Sqlite Connection, Data Record Interface (For Sure)

IMPORTANT WHEN BUILDING

You need to set the Player API Compatibility Level to “.NET 4.0” in the “Other” Settings and you need to copy the “System.Data.dll” and “Mono.Data.Sqlite.dll” files from your Unity install folder and put it in your projects “Plugins” folder of the build will fail.

Example Install folder for system.data.dll location: “C:\Program Files\Unity\Editor\Data\MonoBleedingEdge\lib\mono\unityjit”

Using SQLite in Unity allows for full database access without having to send out external information or commands, as the database in stored with the game or app. This is not meant for secure information as anyone can open the database.

I haven’t tried any of this in BOLT yet but I will add to this post after I have tried it.

Steps to get setup

  1. Create a new folder under Assets called “Plugins” if there isn’t already one there.
  2. Download SQlite
  3. Copy “sqlite3.def” and “sqlite3.dll” into that Plugins folder you just created.
  4. Download an SQLite database manger program
  5. User the database manager program to create a new database in your projects “Assets” folder. (Make note of the full name of this database file)
  6. Copy “System.Data.dll” and “Mono.Data.Sqlite.dll” from your Unity install path + ” \Unity \Editor\Data\Mono\lib\mono\2.0 ” and paste them into the Plugins folder that you created.

If you are using Bolt in the project some of these files are already included and will give you an error in teh console until you remove the duplicate. Just delete the files mentioned as being duplicated in the console and everything will still work.

When used with Bolt.

You are now ready to create the C# code to connect to the database. Here is an example.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Mono.Data.Sqlite;
using System.Data;
using System;


public class databasetest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string conn = "URI=file:" + Application.dataPath + "/leaderboard.db"; //Path to database.
        
        Debug.Log(conn);
        IDbConnection dbconn;
        dbconn = (IDbConnection)new SqliteConnection(conn);
        dbconn.Open(); //Open connection to the database.
        IDbCommand dbcmd = dbconn.CreateCommand();
        string sqlQuery = "SELECT score_id, player_name, player_score " + "FROM Leaderboard_Table";
        dbcmd.CommandText = sqlQuery;
        IDataReader reader = dbcmd.ExecuteReader();
        while (reader.Read())
        {
            int score_idx = reader.GetInt32(0);
            string player_namex = reader.GetString(1);
            int player_scorex = reader.GetInt32(2);

            Debug.Log("scoreID= " + score_idx + "  name =" + player_namex + "  score =" + player_scorex);
        }
        reader.Close();
        reader = null;
        dbcmd.Dispose();
        dbcmd = null;
        dbconn.Close();
        dbconn = null;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
  1. Replace “leaderboard.db” with the name of your database
  2. Edit “SELECT score_id, player_name, player_score ” + “FROM Leaderboard_Table” to reflect actual columns and tables from your database.
  3. Put this script on an active gameObject in your screen and the Console area should show you the information from your database.

If you are getting complaints about the Table not being found. Take a look at the name of your database and make sure it matches your code. My code said “Leaderboard.s3db” but my actual database was called “leaderboards.db”

SQLite creates a new empty database if it fails to find the one you specified in code, and that database will be completely empty, leading to the lack of finding a table.

Inserting data into the database

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Mono.Data.Sqlite;
using System.Data;
using System;


public class database_senddata : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string conn = "URI=file:" + Application.dataPath + "/leaderboard.db"; //Path to database.

        //Debug.Log(conn);
        IDbConnection dbconn;
        dbconn = (IDbConnection)new SqliteConnection(conn);
        dbconn.Open(); //Open connection to the database.
        IDbCommand dbcmd = dbconn.CreateCommand();
        
        // Getting Info From Database
//        string sqlQuery = "SELECT score_id, player_name, player_score " + "FROM Leaderboard_Table";

        // Sending Info to Database
        string sqlQuery = "INSERT INTO Leaderboard_Table (player_name, player_score) VALUES ('newentry','1000')";

        dbcmd.CommandText = sqlQuery;
        IDataReader reader = dbcmd.ExecuteReader();

        // Getting information from the database **************************************
        //        
        //        while (reader.Read())
        //        {
        //            int score_idx = reader.GetInt32(0);
        //            string player_namex = reader.GetString(1);
        //            int player_scorex = reader.GetInt32(2);
        //
        //            Debug.Log("scoreID= " + score_idx + "  name =" + player_namex + "  score =" + player_scorex);
        //        }
        //
        // ****************************************************************************


        // Sending information to the database Just requires the sqlQuery be changed to an INSERT

        reader.Close();
        reader = null;
        dbcmd.Dispose();
        dbcmd = null;
        dbconn.Close();
        dbconn = null;
    }

    // Update is called once per frame
    void Update()
    {

    }
}

This code is the same as above except the reading of data has been commented out and an INSERT command had been put in it’s place. This allows us to add information to the database.

Here are the data types for SQLite