[Plugin] SQLite3 Database Plugin

Hi there! This is my first attempt at a plugin, I needed this myself for my little project and decided to share it with everyone.
It’s not complete and I intend to add features to it as the need arises. Basic update and insert functionality will probably come next (for now only reading data is supported).
I’m still quite new to UE4 so pardon for any mistakes or bugs in the code. I have tested this with a couple of test projects and it seems to have worked fine. I feel this is now matured enough to share it with you.

Project site on Github

https://github.com/Jusas/SQLite3UE4

What is it?

A plugin for UE4 that provides code and blueprint functionality that enables you to use SQLite3 databases in your projects.

Currently only supports reading data from databases. Updates and inserts will come next. By using reflection it is possible to get data directly into member properties of C++ classes and blueprints, provided that the database field names match the UObject property names and that they have compatible data types. By using a normal query you can get data from tables into an array of string key -> value pairs. Both C++ and blueprints are supported. For convenience, queries can be constructed from nodes in blueprints (easier to plug in variables into queries) or entered directly as a string. From C++ you’re expected to use the string version of the methods.

Currently supported platforms

The current version was built with Unreal Engine version 4.6.1.

Win32 and Win64 platforms are currently supported and tested. Mac and Linux should work as well but requires compiling the sqlite3 binaries and editing the build script so that the compiler can find the correct libraries to link to. Contributions on this would be much appreciated.

How to install

For the convenience of testing and providing a sample of its usage, the plugin is inside a sample project in the repository (/Plugins/SQLite3UE4). Download the repository and just copy that folder into your own project’s Plugins folder and you should be set.

How does it work?

The plugin is pretty straightforward. The steps to get your data are:

  • Register a database to the plugin
  • Construct a query from blueprint nodes or by supplying the SQL query string
  • Plug in the query to a Get Data method and you’re done

It’s really simple.

Sample usage

Blueprints

Here’s a screenshot of a query setup with blueprints.

With the database table looking like this:

C++

And here’s a simple sample in C++:

Header:


UCLASS()
class SQLITE_API AMyActor : public AActor
{
    GENERATED_BODY()


public:

    UFUNCTION(BlueprintCallable, Category = "My Actor")
    bool GetMyStats();

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Actor")
    FString Name;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Actor")
    int32 Age;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Actor")
    FString Gender;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Actor")
    float Height;


};

CPP:


#include "MyActor.h"
#include "SQLiteDatabase.h"

bool AMyActor::GetMyStats()
{
    FString dbName = TEXT("TestDatabase");
    FString actorName = TEXT("Bruce Willis");

    if (!USQLiteDatabase::IsDatabaseRegistered(dbName))
    {
        USQLiteDatabase::RegisterDatabase(dbName, "Databases/TestDatabase.db", true);
    }

    bool didPopulate = USQLiteDatabase::GetDataIntoObject(dbName, FString::Printf(TEXT("SELECT Name, Age, Gender, Height FROM Actors WHERE Name = \"%s\""), *actorName), this);

    return didPopulate;
}

I hope you find this plugin useful! It was certainly made with re-usability in mind :slight_smile:

Big fan of sqlite, this looks perfect.

Hi,

I think a SQLite plugin has already been merge onto the Unreal Engine 4.6.

Not sure from where it came, but look at the following other forums threads:

Cheers!

Hi,

I think a SQLite plugin has already been merge onto the Unreal Engine 4.6.

Not sure from where it came, but look at the following other forums threads:

Cheers!
[/QUOTE]

Whoa! Lots of stuff!
Well, good thing that they’ve sneaked SQLite in. I’ll have to start using that internally. However I believe my plugin can still offer some extra beef on top, so I’ll concentrate on delivering a useful plugin :slight_smile:

In any case, I would be interested to look at it!

In the past I’ve done some work with my own SQLiteC++ wrapper

Hi Jusasi,

Thanks for your work on this plug-in. I’m interested in getting it to work on OS X. Been studying how to use the build system and scripts. Haven’t successfully got this working on the mac just yet but I’m hoping too soon, perhaps with your assistance. Also been checking out the “built in” version of sqlite, but haven’t got that working yet either. Have you done anymore work on this yet? I’m interested in being able to both read and write to the sqlite database. I’m building a health and lifestyle app in Unreal Engine and need to build a food and exercise database as you might imagine.

Thanks again for your contributions!

Jason W.

I think my problems stem from not building the sqlite binaries correctly. I tried using the unix command line as described in other posts, but perhaps it would be better if I downloaded the engine source and let the build system compile them along with the engine as instructed in setting up the built in module. Then maybe I could move the compiled sqlite binaries to your plugin’s Third party folder and then edit the platform and file name in the build script. What are your thoughts on this? Any advice for a newbie? Will your plugin work ok from a binary version of the engine launched from the Launcher? Will it work with 4.7.5?

Thanks for any help you can offer me!

Jason W.

Hi JasonW,
Having to build the binaries must be no doubt a bit annoying. I had thought of building them myself but I didn’t have an OSX machine available at the time. It shouldn’t be a big deal but I haven’t dealt with OSX compiling so I don’t know what the real problem is. Perhaps it has got something to do with 64bit/32bit binaries? The editor is 64bit, are you building the SQLite library as 64bit? You could probably skip the command line building and create a project in XCode and build it there if necessary, should be a more user friendly environment.

Unfortunately what comes to the writing into SQLite databases I never got into implementing that I’m afraid. I moved on to use Unity as my own platform and that killed my need to develop this plugin any further. In practise however it should be pretty easy to enable that functionality with just minimal changes, since there’s already support for free form queries if I recall correctly. It’s just that there aren’t custom nodes for that purpose. I’ll maybe take a look at it if I have the time.

Oh, also take a look at the build C# script, you should name the built library correctly in order for UE4 build system to find it.
EDIT: to be precise, this line needs changing since it doesn’t account for OSX builds

Hi Jusasi,

Thanks so much for the reply and the offer to help. Sorry to hear you left for Unity! haha

So, I finally got this working on the Mac platform and redesigned the build file a bit to account for various platforms and make it easier to set things up. I would also like to add support for IOS and Android later on, since the app I’m building will be deployed on several platforms. I even got your test project running on the Mac and was able to try out the test database you set up.

I would really like to make this a flexible plugin because the built in SqliteSupport is really hard to setup and use for people like me. Any help you might give would be greatly appreciated. Maybe we could even update your github repository with some of the changes I’ve added for others to use.

Here is a screenshot of the platform directories I made inside of ThirdParty/SQLite3/Lib/

LibPlatforms.png

And here is the edited plugin build file:

I’m getting this error


LogDatabase:Error: SQLite: Unable to add database '../../../../../../Users/MYUSERNAME/Documents/UnrealProjects/Final/Content/Databases/Main.db', it is not valid (problems opening it)!
LogDatabase:Error: SQLite: Unable to get data to object, database validation failed!

Maybe a bug?

Hi Jusasi. Great plugin, got it working fast thanks to the sample project you provide. I’ve got two questions though:

1-) As far as I have tested and understood, for using this with an external project I just drop the Plugin folder, create any Blueprint and it works. Is that correct? Am I missing some crucial part?
2-) What does the BP_MyActor do? It inherits from MyActor (which I can’t find) and just calls GetMyStats. Is that necessary?

P.S.: By the way, I recompiled this using 4.7.6 and encountered no bugs so far. Thanks a lot for providing this with the source!

I used SQLite3 Plugin in 4.8.2.

There is no error while building and blueprint compilation. But I found the build failure in packaging (windows-64) and launching. More detail with log file is here.

Hey Jusasi,

Thanks for all the information you posted; im totally new to all of this but this helps a lot.

Where did you create your database that have you pictured here? I am trying to link a database that would be similar to that, but don’t know where to create it. The hopes would be to create a database that can store login information of players for my game. Thanks for any info!

We hope this plugin can help you.

HiSQLite3 (SQLite3 For UE4)

Thanks…I hope we can use in blueprint…but still thanks so much