Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity with MySQL

Discussion in 'Scripting' started by zumwalt, Oct 11, 2010.

Thread Status:
Not open for further replies.
  1. SevenUnearth

    SevenUnearth

    Joined:
    Jun 26, 2013
    Posts:
    8
    I still have a problem with the code and I can't find the answer anywhere.

    I'm trying to use Connection.State and my con.State but both are said as they have been unloaded. Does it mean I have a problem with my System.Data.dll...maybe the version is not good. I'm using the one in unity in the folder 2.0

    If anybody knows what is going on. I would like to know.
     
  2. zBlaze

    zBlaze

    Joined:
    Sep 9, 2013
    Posts:
    2
    Hey guys,

    I'm having the error that's been mentioned alot before: "Assets/MySQLJS.js(223,32): BCE0005: Unknown identifier : 'MySql'."

    I have a folder called Plugins directly in Assets folder containing the DLLs:
    - MySQl.Data.dll (I got this one from C:\Program Files (x86)\MySQL\MySQL Connector Net 6.6.4\Assemblies\v2.0\)
    - System.Data.dll
    - System.Drawing.dll

    So I have no idea what's wrong. Hope anyone can point that out for me.

    Thanks in advance.


    Regards,



    zBlaze
     
  3. Ghidera

    Ghidera

    Joined:
    Mar 6, 2013
    Posts:
    53
    I just revisited this after having it working fine a year or so ago and got the unknown identifier error on a project that used to work fine. At the same time I noticed mono didn't like my older project scripts line endings when I edited and saved them. I put two and two together and opened all scripts, put a space in somewhere and saved/converted line endings, and the problem went away.
     
  4. joelm27

    joelm27

    Joined:
    Feb 4, 2016
    Posts:
    32
    Hello

    does sqlite database can be hosted on google cloud SQL?, cause i checked it format it mySQL. But we finished with our sqlite database the only thing which is left is to find a way around it. We are using unity3 5.3.... Any idea
     
  5. robertono

    robertono

    Joined:
    Jun 3, 2014
    Posts:
    22
    Seems yes, because connection from external applications is allowed.
    https://cloud.google.com/sql/docs/external
     
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    way to grave dig.

    FYI, if you're going to use mysql/c# this way without using php query's using the www class, you need to ensure that your server IP doesn't change. Most cpanels you try to connect to will prevent unknown ip connections.

    It's better to access the mysql database through a php query and the www class
     
  7. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    I know old OP, but I'm in need of a MySQL conenction for Unity as well and more specifically the Linux version. Does this one work with Linux as well (because of the .DLL files)?

    [EDIT] It took a bit to get MySQL installed on Linux and move my test database (from a blog, but it is something to test with :D) to the Linux machine, but I can confirm that these .DLL files work perfectly with the Linux version of Unity!
     
    Last edited: May 23, 2016
  8. robertono

    robertono

    Joined:
    Jun 3, 2014
    Posts:
    22
    If DLL is Managed then it could be used on most platforms. If DLL is Native, it is for specific OS, as I know.
     
  9. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Good package, but I'm missing 2 very important instructions that are NOT MySQL native, but available in PHP: addslasles() and stripslashes(). Their function is to "Returns a string with (or without) backslashes before characters that need to be escaped. These characters are single quote ('), double quote ("), backslash (\) and NULL (the NULL byte).". Not using these functions will result in weird data in your database (as I've seen in my own PHP/MySQL project). Luckily these functions are very easy to write...

    I'm no expert on .DLL usage (actually first time using them), and these DLLs seem to be managed, even though there are developer packages available for different platforms. And honestly I think it's a good thing they are managed, because it'll make it a lot easier to use them cross-platform.
    In my case I can write all code on my Windows machine and when I'm done export the server-side project to the Linux machine to compile it there and work on it some more...

    [EDIT] about addslashes() and stripslashes()... These functions have changed over time quite a lot.
    In the older versions of PHP addslashes() added a \ before ', ", \ and null.
    In the current version addslashes() adds an extra \ and ' to the \ and ' resp. and just ignores the " completely. I'm not sure what's done with null though.
    The stripslashes() is bugged when it comes to \\ processing. The function simply removes ALL instances of \ from the string. This problem is known to PHP.net but totally ignored since 2008 because it's "working as intended"...
     
    Last edited: May 24, 2016
  10. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    I've been brushing up my PHP/MySQL knowledge (it's been almost a decade since I got my certification and half that time since I last worked with it) and an other update for addslashes() & stripslashes() is in place...
    When using the query:
    Code (csharp):
    1. string UserName="Alex'Erne"; // Yeah that's me ;-)
    2. "SELECT * FROM users WHERE UserName='UserName'"
    then the MySQL database will trigger an error because the result will look like:
    Code (csharp):
    1. "SELECT * FROM users WHERE UserName='Alex'Erne'"
    and everything after UserName='Alex' will be seen as the next part of the WHERE statement. Instead you should use:
    Code (csharp):
    1. string UserName=addslashes("Alex'Erne");
    2. "SELECT * FROM users WHERE UserName='UserName'"
    In this example the $UserName will come out as "Alex\'Erne".

    The above is true in PHP for both SELECT and INSERT MySQL statements. When I dump my database, the UserName field shows it to be 'Alex''Erne' (new workings of addslashes in PHP as I said in my previous reply.

    Now hop to Unity C# where I wrote the following AddSlashes() method:
    Code (csharp):
    1.     string AddSlashes(string myInString) {
    2.         string myOutString="";
    3.  
    4.         for(int i=0; i<=myInString.Length-1; i++) {
    5.             if(myInString[i].Equals('\\') || myInString[i].Equals('\'') || myInString[i].Equals('"') || myInString[i].Equals(null)) {
    6.                     myOutString+="\\";
    7.             }
    8.             myOutString+=myInString[i];
    9.         }
    10.         return myOutString;
    11.     }
    When again I use the query "SELECT * FROM users WHERE UserName='UserName'" (converted to a bit of C# code of course ;) ) without my written AddSlashes() it'll trigger the same error as in PHP. Using AddSlashes("SELECT * FROM users WHERE UserName='UserName'") will result in the right behavior of the query.

    When writing to the database with an INSERT command things will get odd in Unity. Where in PHP you MUST use addslashes(UserName) with the INSERT query as shown below:
    Code (csharp):
    1. string UserName=addslashes("Alex'Erne");
    2. "INSERT INTO users (UserName) VALUES (UserName='UserName')"
    Without the addslashes() in PHP, you'll get the same error as when not adding it for the SELECT command.

    In Unity though, you MUST NEVER use AddSlashes() when using an INSERT query command. Oddly enough when following OP's code to insert, the UserName string is accepted like there's nothing weird there for MySQL. From the looks of how the DLL works and values being placed into a List<> instead of processing a string as PHP does might be the reason that the string "Alex'Erne" is processed normally.

    When you accidentally DO use my AddSlashes() when writing to a database your data WILL BE SCREWED AND DATA CAN NOT BE RECOVERED WITH A QUERY! I have experimented with it and here's the results of usage writing with and without my AddSlashes().
    Using AddSlashes("Alex'Erne") results into "Alex\\''Erne" in your database while not using it your database will result into "Alex''Erne".
    The problem here is that when using the AddSlashes("SELECT * FROM users WHERE UserName='UserName'") with UserName being "Alex'Erne" will ALWAYS return the record holding "Alex''Erne" in your database. The only way to retrieve the data in the "Alex\\''Erne" record is by setting UserName to "Alex\'Erne" and that is IMPOSSIBLE in C# because \' will automatically be converted to a single quote by Unity in your string...

    ...feel free to use the AddSlashes() code when using MySQL with Unity. I donate the code hereby to the Public Domain...
     
  11. Lohoris2

    Lohoris2

    Joined:
    Aug 20, 2013
    Posts:
    86
    I'm under the impression that this plugin includes a DLL from dev.mysql.com which is GPL-licensed, correct?

    In that case you should state it very clearly both in this post and in the plugin itself.
    I hope you don't want people to mistakenly include in their closed-source projects some GPL code…

    Please be very careful with this sort of thing.
    Always include a proper license in your released software, otherwise it will be unusable by anyone who is doing anything serious.
     
  12. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Welcome to the internet bro. License means nothing.
     
  13. Lohoris2

    Lohoris2

    Joined:
    Aug 20, 2013
    Posts:
    86
    Yeah, sure.

    Explain that to your lawyer when someone decides to sue you, then please post his reaction on youtube.
     
    KelsoMRK likes this.
  14. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    okay bruh. #IUseIllegalSoftwareCommercially ;) and man am i rich off it
     
  15. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Please do not advocate ignoring licenses or using software illegally- what's wrong with you?
     
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,254
    There's zero tolerance for illegal software usage here. Anyone on this forum should know better.

    --Eric
     
    Tea_Man88, Buhlaine and TaleOf4Gamers like this.
Thread Status:
Not open for further replies.
unityunity