MySQL Integration is a plugin that lets you connect your UE4 project to your MySQL server, and store and retrieve data from the server via SQL queries, directly from the Blueprint. This plugin brings the power of C#.NET in Unreal Engine 4.

If you have worked with SQL, you must be very familiar to Select, Update, Insert and Delete SQL queries, joins and sub-queries. If you know C#.Net, and worked with ADO.Net, you must be knowing how to set Connection properties for your Server, and how to write and execute your SQL Queries within your .NET applications.

But now you can use Blueprints in Unreal Engine 4 to do your job. You are still writing the same queries that you are familiar with, but here you are writing it within Blueprints, which gives you flexibility to store and retrieve data as well as Images from your MySQL Server to your UE4 projects.

To get started, first ensure that the plugin is installed, by visiting the Plugin Window, as shown below:



If you are unsure regarding how to work with MySQL, you can visit the below link to get yourself started with MySQL

https://dev.mysql.com/doc/mysql-getting-started/en/

Here I will be explaining you in details how to set up your database connection and how to write and execute SQL queries from UE4.

Setting Up Database Connection in UE4

Before we begin writing our SQL queries and executing them in our database, we first need to set up a connection within UE4. This is a onetime setup, and is similar to how we manually connect to our DB Server using Windows or MySQL Server Authentication, as shown in the above steps.

Authentication Modes

To set up a DB Connection, we either need to use Windows Authentication mode or MySQL Authentication mode.

Windows Authentication mode authenticates users to use the Database server via Windows Login ID and Password.

Function Names
Function Descriptions
Get Current User
This function returns the current Windows User Name.
Get Current Domain
This function returns the current PC Domain.
Authenticate User
This function takes the current user name, domain and password for your Windows account as input and returns the authentication result as a Boolean variable. Please note that this function will not work if the current Windows user does not require any password to login. So you may want to avoid using this function unless absolutely necessary.

MySQL Authentication mode uses logins that are created and stored in MySQL Server and are not based on Windows user accounts. Here you need to explicitly enter both your MySQL Server ID and password, while setting up the connection property.

To learn about Authentication modes in detail, you can refer to the below link:

https://dev.mysql.com/doc/refman/5.5/en/authentication-plugins.html

MySQL SERVER CONNECTION

To connect to the MySQL Server, we first need to set the connection properties. For that, we need to call the function Set Connection Properties, which forms a valid connection string that will later be used to access the given database. If you are using Windows Authentication, you don’t need to enter Used ID or Password.

Inputs
Descriptions
Server
Here you need to enter you Server address. If the server is your local machine, then it is localhost by default. If you have hosted your server in your web service, you must be having your dedicated server name. You need to enter that here.
DBName
This is your Database name where you want to set up your initial connection.
User ID
For MySQL Server Authentication, you need to pass your MySQL Server user name here.
Password
For MySQL Server Authentication, this is your MySQL Server password.
Is Trusted
A Windows Authentication is referred to as a trusted connection to the database, since any user can login from their Windows user accounts. This should be checked if you are using Windows Authentication mode.
Outputs
Descriptions
Is Success
This will return true if the database connection has been successfully established, else false.
Error Message
This will be empty if connection to the database is successful, else it will contain the exception message.

Forming the Connection String

A connection string typically looks like:

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

To learn about various types of Connection String and forming the one that best suits your requirement, you can refer to the below link:

https://www.connectionstrings.com/mysql

So if your connectionstring consists of extra parameters , apart from the usual parameters included in the Set Connection Properties function(Server , DBName , UserID , Password , IsTrusted) , you can enter those extra parameters in the Server input, seperated by a semicolon. For example , if your connectionstring is something like :

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Protocol=pipe;PipeName=mypipename;

Then you need to enter , in your Server input , myServerAddress;Protocol=pipe;PipeName=mypipename

Writing SQL queries within UE4 Blueprints

Once the connection property has been set, you can now start forming your SQL queries. Forming queries can be categorized in two types, queries that modify data to the database (INSERT, UPDATE, DELETE), or the queries that fetch data from the database (SELECT). We will discuss these two categories one by one.

Let us say we have a created a table in our database called Products by using the below query:

CREATE TABLE dbo.Products
(
ProductID int PRIMARY KEY NOT NULL,
ProductName varchar(25) NOT NULL,
Price money NULL,
ProductDescription text NULL
)
GO

We will take this table as a reference to form our queries and discuss the two categories of query formation.

UPDATE QUERIES

To modify data in our table Products, we will use the function Update data from Query which takes a string input where we have to enter our query and executes the query in the database.

A typical way of forming a simple Insert query is shown below.

SELECT QUERIES

To bring the data from SQL Server to your game, you need to call the function Select data from Query. This will take your select query as a string input.

Outputs
Descriptions
Result by Column

This will return an array of structured variable called SQLDataTable. This represents a column in the table and contains two parameters, ColumnName and ColumnData.

This will return an array of structured variable called SQLDataTable. This represents a column in the table and contains two parameters, ColumnName and ColumnData.

ColumnName

ColumnName contains the header name of the column of the current instance of our initial array that we get by executing our select query. If you want to display data of a specific column, you can use this variable to match the column name.

ColumnData

ColumnData is an array of string consisting of all the elements of a given column. So you can easily set up a for-loop here to get all the elements of a particular column iteratively.

Result by Row
This contains an array of structured variable called SQLDataRow. This structure contains an array of string that represents values of an entire row. So the number of elements in this string array equals number of columns, and number of elements in the Result by Row array equals number of rows in the output of the Select query.