Working in Godot with SQLite and C#

Here are the steps I took to get SQLite to work in a C# project.

1. Create a new project.

2. Add a plain Node.

3. Rename the node to “Database” and save the scene as Database.tscn.

4. Right click the node and choose attach script. Make sure you set the language to C#. Godot will generate the necessary C# solution files and a skeleton script called Database.cs.

5. Open the C# solution file using the IDE of your choice. For my project I’m using JetBrains Rider.

6. Next we’ll need to add a SQLite package. Because I want the final project to have cross-platform potential, I installed the SQLite package Mono.Data.Sqlite.Portable package via Nuget. See: https://www.nuget.org/packages/Mono.Data.Sqlite.Portable

7. You’ll also need to add the System.Data.SQLite package to allow C# to access the database. It’s available here if you do not already have it available on your system: https://www.nuget.org/packages/System.Data.SQLite

8. Next we’ll need to add some code to the C# script.

using Godot;
using System.Data.SQLite;

public class Database : Node
{
    public override void _Ready()
    {
        SQLiteConnection dbConnection = new SQLiteConnection("Data Source=database.db;Version=3;");
        dbConnection.Open();

        string sql = "create table highscores (name varchar(20), score int)";
        SQLiteCommand command = new SQLiteCommand(sql, dbConnection);
        command.ExecuteNonQuery();

        sql = "insert into highscores (name, score) values ('Eric', 9002)";
        command = new SQLiteCommand(sql, dbConnection);
        command.ExecuteNonQuery();

        dbConnection.Close();
    }
}

9. Now we can execute the project. Important: you must run the whole project and choose the Database node as the main scene for the export steps below to work correctly. When you run the program, all you’ll see is a blank Godot engine window. As long as it doesn’t crash that’s a good sign. Go ahead and close the window.

10. In Rider I can configure the SQLite database as a data source. Viewing the data with SELECT * FROM highscores; shows the desired results: the database was automatically created, then the highscores table was created, and a row successfully inserted. Cool!

11. So what if we want to export the project for distribution? To test this, I prepared an export for my Windows Desktop. On the menu bar go to Project -> Export and then click the Add button. If you haven’t already set up an export template, you’ll be prompted to download one. The Windows Desktop export preset is about a 500mb download, and will install automatically.

12. Choose an export path in the top right corner of the window. By default it will just place the export in the project directory, but you probably should put it in a new directory. I created a folder called export and chose that. Click the Export All button and choose Debug to build a runnable .exe file.

13. NOTE: I don’t yet have a lot of experience in exporting projects, so scripting the following process is likely possible. I just don’t quite know how to do it yet!

14. Your exported application will not run quite yet because it’s missing the SQLite library files. First, you’ll need to find the SQL.Interop.dll file and add it to the exported project directory. I found my copy here: C:\Users\eric\.nuget\packages\System.Data.SQLite.Core\1.0.97\build\net451\x64. There may be other SQLite.Interop files available in nearby directories. The date of the interop file matches the date of the Nuget package you installed earlier. Copy this file and put it into your exported project folder. More information about the Interop file is found here: http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

15. Next copy over the System.Data.Sqlite file located C:\Users\eric\.nuget\packages\System.Data.SQLite.Core\1.0.97\lib\net451 to your export directory.

16. (OPTIONAL) If it doesn’t already exist your database should be created on execution of the application. But if you have trouble you may need to copy your database.db over to your export directory. For a Linux export, see this link: https://stackoverflow.com/questions/21293105/system-dllnotfoundexception-on-mono-sqlite

17. Run the executable. It ought to execute correctly at this point with a stable blank window. If you run the application over and over, the SQL defined in the .cs file will be inserted multiple times. You can verify this by viewing the table. NOTE: be sure to examine the correct database file if you’ve exported the project to a different directory.

We now have a viable C# Godot project that can interact with SQLite. Give yourself a pat on the back!

Eric
Eric

Code guy.

Articles: 8

Leave a Reply

Your email address will not be published. Required fields are marked *