MYSQL & Unity, Where to get the libraries to reference?
Hey guys. Theres a lot on MSQL and unity out there in the forums.
But they all have broken links to demo projects and libraries needed. I cant even begin trying the tutorials people offer because I cant reference the libraries in mono develop.
There are asset store items for MySql but even there the links to documentation or demo's etc are broken. No documentation , contact, or website. So I'm hesitant to spend any money when they cant even keep their website references on the asset store up to date.
Ive never tried using Mysql in unity & there seems to be no way to begin.
I see a lot of people using DLL's or unity imports in their tutorials that I cant find anywhere.
Also this is an Android project. Are the DLL's I need even going to work once I deploy the project on to an android device?
Any advice on where to begin?
Answer by remekgc · Mar 03, 2020 at 08:57 PM
Hello there, I have faced the same problem yesterday and I've found a satisfying solution that works both on PC and Android.
Step 1: Download .DLL file from here matching your Visual studio project target .NET version (for me 3.5, version 6.9.8.0 worked just fine). If you download a wrong version you will get an error in Unity editor. links to download the file: https://www.dllme.com/dll/files/mysql_data_dll.html or this one: https://downloads.mysql.com/archives/c-net/
Step 2: Unpack the .DLL file and include it in the project (put it anywhere inside of the Assets folder).
Step 3: Program your connection to the database ;) here is a short example:
using System; using System.Data; using MySql.Data; using MySql.Data.MySqlClient; public class Tutorial4 { public static void Main() { string connStr = "server=localhost;user=root;database=world;port=3306;password=******"; MySqlConnection conn = new MySqlConnection(connStr); try { Console.WriteLine("Connecting to MySQL..."); conn.Open(); string sql = "SELECT COUNT(*) FROM Country"; MySqlCommand cmd = new MySqlCommand(sql, conn); object result = cmd.ExecuteScalar(); if (result != null) { int r = Convert.ToInt32(result); Console.WriteLine("Number of countries in the world database is: " + r); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } conn.Close(); Console.WriteLine("Done."); } }
Answer by Da_Elf · Jun 27, 2020 at 06:24 PM
Im using unity 2019 and i can get the connection to open. And using the ExecuteScalar() with a COUNT() seems to work just fine. However when i try to do "SELECT username FROM users" and using other OLD tutorials I used the code below I get a System.TimeoutExeption: error
// TEST 2
Debug.Log("Test 2");
string sql2 = "SELECT username FROM users";
cmd = new MySqlCommand(sql2, con);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Debug.Log(rdr[0]);
}
rdr.Close();
So i tried something else. "SELECT * FROM users" And this time i get a different error. . .
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in :0 > at MySql.Data.MySqlClient.CharSetMap.GetCharacterSet > (MySql.Data.Common.DBVersion version, > System.String CharSetName) [0x00000] > in > :0 > at MySql.Data.MySqlClient.MySqlField.SetFieldEncoding > () [0x0004b] in > :0 > at MySql.Data.MySqlClient.MySqlField.set_CharacterSetIndex > (System.Int32 value) [0x00007] in > :0 > at MySql.Data.MySqlClient.NativeDriver.GetColumnData > (MySql.Data.MySqlClient.MySqlField > field) [0x000ad] in > :0 > at MySql.Data.MySqlClient.NativeDriver.GetColumnsData > (MySql.Data.MySqlClient.MySqlField[] > columns) [0x00004] in > :0 > at MySql.Data.MySqlClient.Driver.GetColumns > (System.Int32 count) [0x0001c] in > :0 > at MySql.Data.MySqlClient.ResultSet.LoadColumns > (System.Int32 numCols) [0x00000] in > :0 > at MySql.Data.MySqlClient.ResultSet..ctor > (MySql.Data.MySqlClient.Driver d, > System.Int32 statementId, System.Int32 > numCols) [0x00029] in > :0 > at MySql.Data.MySqlClient.Driver.NextResult > (System.Int32 statementId) [0x00035] > in > :0 > at MySql.Data.MySqlClient.MySqlDataReader.NextResult > () [0x00053] in > :0 > at MySql.Data.MySqlClient.MySqlDataReader.Close > () [0x00020] in > :0 > at MySql.Data.MySqlClient.MySqlCommand.ResetReader > () [0x00020] in > :0 > at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader > (System.Data.CommandBehavior behavior) > [0x002d1] in > :0 > at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader > () [0x00000] in > :0 > at (wrapper remoting-invoke-with-check) > MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() > at DatabaseHandler.Awake () [0x00176] in C:\Users\St Francis > 01\Desktop\AGD\MySQL\Link To > MySQL\Assets\Scripts\DatabaseHandler.cs:68 > > UnityEngine.Debug:Log(Object) > DatabaseHandler:Awake() (at Assets/Scripts/DatabaseHandler.cs:77)