Search Unity

MySQL Connection in Unity issues

Discussion in 'Scripting' started by TheUKDude, Feb 25, 2017.

  1. TheUKDude

    TheUKDude

    Joined:
    Jul 27, 2013
    Posts:
    72
    I am writing a dedicated server that runs on Linux that needs to access a MySQL Database on the same machine.

    I have downloaded MySQL.Data (loads of different versions) for up to .NET 3.5, which Unity accepts fine and I am able to reference it in Visual Studio fine.

    Now when I add in the connection string in the code it fails with the following Error:
    • KeyNotFoundException: The given key was not present in the dictionary.
    • System.Collections.Generic.Dictionary`2[System.String,System.Object].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
    • MySql.Data.MySqlClient.MySqlConnectionStringBuilder.get_Database ()
    • MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString (System.String value)
    • Server.Start () (at Assets/Scripts/Server.cs:47)


    I am using the following connection string:
    "server=localhost;uid=USER;pwd=MYPASSWORD;database=MYDATABASE;"

    I am like pulling my hair out due to the key names do exist due to I check in the MySQL.Data source code.

    Has anyone managed to get MySQL Connections working in Unity, if so what version are you using and what connection string are you using.

    Thanks

    Paul
     
    Lohoris2 likes this.
  2. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    Why don't you simply use the provided classes from Unity? It might also be better to use external php files to connect to your database as php files can't be viewed by other persons and your connection data is save. you simply need to use the php file as parameter and wait for the results, pretty simply.

    https://docs.unity3d.com/540/Documentation/Manual/UnityWebRequest.html
     
  3. TheUKDude

    TheUKDude

    Joined:
    Jul 27, 2013
    Posts:
    72
    Erm because Unity doesn't provide a MySQL Connector.

    Use a HTML web request to a webpage for thousands of SQL Queries?
    Your joking right?

    Paul
     
    Lohoris2 likes this.
  4. TheUKDude

    TheUKDude

    Joined:
    Jul 27, 2013
    Posts:
    72
    @SlyRipper
    Sorry if I sounded harsh in my last reply but I have been up for the last 38 hours trying to get this thing working.

    And having the dedicated server for a FPS that's already using NetworkTransport then also use WebRequests to call a PHP page via an Apache webpage just for the MySQL Queries isn't a good route to take.

    Now if it was a Turn Based Game it "might" or been ok.

    I have now resolved the issue, it was down to something wrong with System.Data.
    I used the version found here: \Editor\Data\Mono\lib\mono\unity\System.Data.dll and it works fine on Windows and Linux.

    So Yay.

    Now to back it up so I done kill it :p

    Paul
     
  5. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    Glad to hear, that you solved it :)

    Well, I'm using WebRequest for my queries and never had any serious problems, but I might try some more ways and check for performance issues, but usually php is pretty fast in processing queries, and if you gather them async in unity there should be less problems ;) At least I'm using them with lazy loading and had no problems with it so far ^^
     
  6. palomo9314

    palomo9314

    Joined:
    Dec 14, 2014
    Posts:
    8
    @Paul-Kirby I just change the System.Data file for the one in the \Editor\Data\Mono\lib\mono\unity\System.Data.dll but it gives the same error
     
  7. palomo9314

    palomo9314

    Joined:
    Dec 14, 2014
    Posts:
    8
    forget the last commen, i had to reopen unity, now it works