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.
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.
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.
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
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.
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
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.
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.
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.
This will return an array of structured variable called
This will return an array of structured variable called
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 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.