Press "Enter" to skip to content

Unity 3D to IBM DB2 Connection Tutorial

logo-ibm-db2I recently had the challenge of connecting to an IBM DB2 database via Unity. After some struggles with setting up DB2 and getting the user access all correct, it ended up being fairly easy. This quick tutorial will show you how to do it and point out some of the pitfalls you might encounter along the way.

Step 1 – Set up IBM DB2

To try this out you’ll need to be able to connect to a DB2 database. I didn’t have access to any so I set one up on my Windows 10 PC. Any of the DB2 installations should work fine for this, but for my task, I went with IBM DB2 Express-C. That’s their free community edition. It’s probably missing some more advanced features, but I really just needed a simple database set up to test the connection to.

The installation isn’t difficult, but the installer UI looks like something out of the 90s for Windows 3.1. Basically, just follow the prompts for the typical installation. You’ll be asked to create a user and password. This actually creates a new user on the Windows OS. It wasn’t clear that would happen at the time of installation and I probably could have gone with my standard Windows user credentials. This is an important factor to note. After setup this account that you choose is the one with default rights to create the database.

So after lots of digging around, I found out this was a problem I had and was slowing me down. Eventually, I found out that the Windows user you’re logged in as has to belong to a user group that DB2 set up. It sets up two user groups: DB2ADMNS and DB2USERS. I made it so that my main Windows account belonged to both groups. This can be done via lusrmgr.msc. Right click on the Start Menu, select Run and type in lusrmgr.msc and you’ll get the Local Users and Groups Manager window. In here you can select your username, right click to get to properties, then in the Member Of tab you can add the user to the user group. I think that only DB2ADMNS group is required, but I did both.

Once you get that all corrected you may also have to make some adjustments to DB2’s configuration. This is done via the newly installed DB2 Command Window. I always run the Administrator version so that I’m sure I have all the access I need. Once you launch this you can run the command db2 get dbm cfg. This will print out the config details in the terminal window. If this doesn’t work right away then try db2start, which starts the DB2 service. Now you’ll want to change a few of these settings. specifically: SYSADM_GROUP, SYSCTRL_GROUP, SYS_MAINT_GROUP, and SYSMON_GROUP. You’ll want all of these to be set to DB2ADMNS which is the user group in Windows that has full access to modify the database. Use the following command to set these:
db2 update dbm cfg using SYSADM_GROUP DB2ADMNS
Do this for each setting.

Now things should be pretty happy. You can try creating a new database with:
db2 create database YOURDATABASENAME AUTOMATIC STORAGE YES
If all is set up correctly then you should get a success message.

Other useful DB2 commands.

I don’t overly enjoy working via the command line terminal so I went and downloaded IBM Data Studio. It was a pretty simple setup and I just went with the defaults. From there you can create your databases, tables, import data and run queries. One major annoyance I found with this is that when you type a lowercase name for a table or column name this program will surround it with double quotes. Which means that if you create a table named mytable then to query it you’ll need to use “mytable” in your queries. Not cool, IBM, not cool. I eventually just named everything with caps, but Data Studio prepares SQL statements for you that you can edit and remove the unnecessary quotes when creating your tables. I still can’t get Data Studio to create a database, so I do that by command line. Not sure what the problem is, I’ve even tried by running it as Administrator and even in the user account that it set up. No go. Command line, no problem. Go figure…

Also when you set up DB2 you have the option to create a sample database. My installation said this had failed, but after going in to Data Studio the database was present.

Last caveat: You’ll need to make sure that your user has access privileges to the database. Do that in Data Studio via the database’s properties.

Step 2 – Connect from Unity

Now that you have your DB2 installation all working fine (this took me what felt like forever) we can hop on over to Unity and set up access to the database. This part is pretty simple.
I created a fresh Unity project and added a Plugins folder to the project. We’ll need to add two DLL files to this folder:
System.Data.dll
System.EnterpriseServices.dll
Unity-friendly versions of these can be found in your Unity installation folder like this:
E:Unity_5.3.4p5EditorDataMonolibmono2.0
Of course, your directory will be different from mine, but the subdirectories should be the same. Just locate those two DLLs and drag and drop them into your project’s plugin folder. I just left the settings on the marked for “Any Platform”, but I’ve only tested on Windows so I’m not sure if these libraries will work on Mac or Linux. I highly doubt they will work on WebGL, maybe on Android, probably not on iOS (because what does? right?).
Now that’s all done we can finally get to my favorite part: code!
For a test I simply created a new MonoBehaviour class and attached it to an empty GameObject in my scene. The code below provides an example of making a simple query and reading the results as strings. For a real integration of this you’ll want to convert the results to useable object types. A good example of this can be found here. For my purposes it was just good enough to prove a connection worked. And here’s the code:

 

That’s it. Hopefully the code is self-explanatory with all of the comments. I’ve set up a repository of this you can grab from GitHub that has a simple UI so you can do a full build and test it out on various machines or modify it to your own purposes. Check it out here.

I hope this saves someone from the many headaches I encountered over the past few days of trying to get this to work. There are a lot of outdated examples on the internet and none specific to Unity. Next up I have to test this with some other database management systems (DBMS) such as MySQL, SQLLite, Oracle, and SQL Server. Should be a fun time setting those up!

vuzop_video_cover_imageI was testing this for an additional feature of an app I’m working on called vuzop. It is a data visualization tool that allows you to view your data in 3D or with a VR headset. Check out more information on vuzop here.

As always,
Thanks for reading!

Loading

3 Comments

  1. Peter George Mavronicolas
    Peter George Mavronicolas 2022/05/12

    The package in Unity that needs to be installed in order to access:

    System.Data.dll
    System.EnterpriseServices.dll

    is “Input System” in Package Manager

  2. Peter George Mavronicolas
    Peter George Mavronicolas 2022/05/12

    correction. Unity System is incorrect. It clears all compiler errors for a moment and reappeared.

    • Sean Mann
      Sean Mann 2022/05/12

      In newer versions of Unity (2021 at least). You don’t seem to need to add these libraries anymore. I can’t fully test because I no longer have a IBM DB2 database set up and don’t really want to. If you post your full log trace here, Unity version, and platform you’re running on then I could potentially take a look but it compiles for me no problem in 2021.3.2f1.
      Good luck! You might be better off converting the DB to something more popular like SQLLite.

Leave a Reply to Sean Mann Cancel reply

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