Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Unity with MySQL

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

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

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Attached is the package that will save and load from MySQL included is a PDF that describes the process of setting up MySQL, adding a table with fields, and adding a view to read from.

    Original Release Date 10/10/2010

    Updated 10/12/2010
    Package now includes both the completed JS and C# scripts to make this work, the PDF is also updated with the JS code.

    Updated 2/10/2012
    Package updated since the Libraries folder I had contained the DLL's that worked in 3.4 but not in 3.5, instead they are required now to be in the Plugins folder which really is not a bug but how the system should have originally worked, they simply refined how they truly look for unmanaged libraries.

    [attachments removed]
     
    Last edited by a moderator: Oct 25, 2016
  2. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Project now includes both JS and C# (or it should, had issues with the new uploader, let me know if it is missing anything)
     
  3. MisterEd

    MisterEd

    Joined:
    Aug 23, 2010
    Posts:
    12
    I've just browsed the pdf and it looks good. I will try it out soon! :)

    I'm using Unity 2.6 and was wondering if u need diffrent dll files in unity regarding mysql and system.

    Regards,

    MisterEd
     
  4. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Well, the project is made in U3, so you probably can't open it in U2.6, the files needed will be the same ones, you just have to pull the ones out of the Unity Editor folders for the local files from
    C:\Program Files\Unity\Editor\Data\Mono\lib\mono\unity
    System.Data.dll and System.Drawing.dll
    The other file is located in:
    C:\Program Files\MySQL\MySQL Connector Net 6.2.4\Assemblies
    MySql.Data.dll

    Just make sure you get and download the connector for .net
     
  5. MichaelHi

    MichaelHi

    Joined:
    Aug 25, 2010
    Posts:
    7
    Can it work with webplayer?:confused:
     
  6. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    No because Unity does not allow the MySQL library to be deployed with the package.
    I plan on deploying an updated document and package this weekend, but this only works in Mac builds and Windows builds, not web player, not andriod, not iphone, nothing else at the moment until Unity loosens up the security model some, which is highly unlikely.
     
    Last edited: Dec 2, 2010
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Sure you really want to make the database globally accessable? (required for mysql to talk to the database)
    Its normal that you talk to PHP / ASP.NET layers inbetween that provide you with the informations you need (which can be done through WWW)
     
    Ironmax likes this.
  8. MichaelHi

    MichaelHi

    Joined:
    Aug 25, 2010
    Posts:
    7
    Thanks!!
     
  9. tikod

    tikod

    Joined:
    Nov 30, 2010
    Posts:
    19
    I'm trying your package with Unity 3.3 but the javascript return errors : Assets/MySQLJS.js(52,23): BCE0005: Unknown identifier: 'MySql'.

    Have I forget to configure something ? (All .dll are in the Assets/Libraries folder)
     
  10. alienx2

    alienx2

    Joined:
    Nov 19, 2010
    Posts:
    40
    Bump! Same as above problem! ^^^ any problem solve?
     
  11. AJMilsom

    AJMilsom

    Joined:
    Mar 31, 2011
    Posts:
    38
    had a look through the PDF it looked very promising, shame it doesnt work with the web player though, could it not connect to a online database?
     
  12. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    Say your database has user info (username, address, char name, CC?, etc)

    Do you *really* want a web player to be able to access that data? Databases usually don't have very specific 'permissions' settings, and you need to have a very good understanding of database design in order to design a system that was 'safe' for global access.

    For example, at the minimum to set up an 'authentication' system, you would need to set up a internally defined procedure in the database's script language. Then you'd have to set very narrow permissions so that procedure is the only thing visible from the global connection. Then you'd have to consider that most 'community' versions of databases tend to not feel OK with 50+ active connections, so you'd hit that threshold pretty quickly with even a mildly successful game.
     
  13. Senkusha

    Senkusha

    Joined:
    Mar 28, 2011
    Posts:
    98
    I would be interested to know how this works. I noticed in the example code that there are some for() loops and the assigning of variables to object variables. I wonder why is this needed, and why couldn't I just write a simple query statement (SELECT * FROM characters WHERE char_id = ?ID) (I'm assuming that the ? in front of the ID informs the processor this is a variable?)

    Thanks.

    Seems like a lot more work than just writing a PHP script to process the query and then transfer that to an array for JavaScript to parse though.
     
  14. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    I had problems with saving and loading, it came up with some funky errors:
    1 : Items in collection : 2
    2 : Something about iddemo_table doesn't have default value at MYSQL really long stuff
     
  15. syrka

    syrka

    Joined:
    May 20, 2009
    Posts:
    14
    Get anyone the mysql dll's working under unityscript? I can use them with c#, but get only error's (Namespace 'MySql.Data.MySqlClient' not found, maybe you forgot to add an assembly reference? or BCE0005: Unknown identifier: 'MySql'.)
     
  16. Akman

    Akman

    Joined:
    Dec 17, 2011
    Posts:
    1
    Attention!!!
    The attached file "MySQLDemo.unitypackage (532.3 KB, 603 views)" is a VIRUS!
     
  17. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    zumwalt ?!
     
  18. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Thats blurp, its no virus. pushed it through kaspersky and ESET32, no hit, so its pretty surely a false positiv of some half arsed freeware protection (avira or payed viruses like McAfee / norton that claim to be good yet lack enough and even spread fake virus / worms that only they detect)
     
  19. rafrafi

    rafrafi

    Joined:
    Jan 26, 2012
    Posts:
    10
    Hi zumwalt,
    how did I fix the problem in JS script??
    Assets/MySQLJS.js(50,23): BCE0005: Unknown identifier: 'MySql'.
     
  20. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    It appears that the latest version of Unity has removed the native import of libraries to an extent, more stripping I suppose, this was working in prior versions of Unity 3, but the new public beta, the JS doesn't work, looks like a regression bug to me. I know the C# version still works but even though the DLL is in the right place from the package, Unity 3.5 public beta does not work with it an Unity Script.

    EDIT:
    Re-Download the package that or create a folder called Plugins in your asset folder and move all DLL's that are in the Libraries folder to the Plugins folder, wabam, should now work. All these itterations later and they make a change where the unmanaged AND managed are now required to be in the Plugins folder, it used to be that you could put them in your own folders exclusive of the Plugins folder to the bug was really that the unmanaged AND managed libraries were allowed to be anywhere and they fixed that. All DLL's need to be inthe Plugins folder it looks like for 3.5
     
    Last edited: Feb 10, 2012
  21. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,095
    Couple of questions

    1) Is it actually possible to encode a virus in a .unityPackage (wouldn't it be a worm)

    2) Does this mysql integration work on mobile? How is its data persistent saved on mobile?
     
  22. JWPlatt

    JWPlatt

    Joined:
    Mar 8, 2012
    Posts:
    2
    I'd love to see how to implement this on Android. Despite the dire security warnings, it's still a good question, and I need to find such a solution for Android. The security concern does not apply in my situation.
     
  23. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    I saw mention on the 1st page that this wont work for a Webplayer, but that's posts from about 2 years ago.

    Is this still the case?

    If so, that means I can give up all attempts to connect directly to an SQL DB from a Webplayer build?
     
  24. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    A better question would be - Why would you ever want a client to connect directly to a remote database? Especially in this case when MySQL works so well with PHP and you have to deploy a web player to a server anyway.
     
  25. JWPlatt

    JWPlatt

    Joined:
    Mar 8, 2012
    Posts:
    2
    Not really. I see this kind of pedantic, second-guessing "help" all the time. And it's not helpful. What it does is not trust the person asking the question to know their own business. If you want to ask why, FIRST answer their question, THEN ask why. It's much more polite and in good faith.

    In my case, I am exploring the use of Android in a closed, controlled, embedded, proprietary wireless LAN environment where direct connection to the database is not only entirely justified for the application but essential, and where http to a PHP server API would render the very low-power, low-bandwidth embedded network totally useless from being swamped with excessive traffic. I know this because it was well-considered. But I shouldn't have to explain, like a toll booth, to seek help.
     
    Last edited: May 10, 2012
    syscrusher likes this.
  26. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Except that's not the use case he provided (in reality what he provided wasn't a use case at all, but whatever). A lot of project planning methodologies encourage you to ask "Why?" as much as possible to drill down to the root requirement. How can I answer his question if I don't know what he's really asking?
     
  27. kingdutka

    kingdutka

    Joined:
    Feb 1, 2011
    Posts:
    129
    Ignore the following, my PC magically decided to find like 10-20 different versions of the file and the first one I tried works without errors...:
     
    Last edited: May 11, 2012
  28. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    hm. Well, whether it's a good idea or not, I think the question itself was clear enough.

    Here it is again, in case somebody knows (and cares to answer):

     
  29. CaptainExtra300

    CaptainExtra300

    Joined:
    Jul 14, 2012
    Posts:
    130
    question,

    am trying to do a networked game client and server and want to use mysql to hold persistent data from server,

    but i do not want to client to access db directly, will this setup work??
    or is it just for the client to access db directly?


    if not any1 can point me to the right direction.

    thanks
     
  30. riifk1215

    riifk1215

    Joined:
    Jul 24, 2012
    Posts:
    9
    Thank you very much, I just need it.
     
  31. ShanksDu

    ShanksDu

    Joined:
    Aug 13, 2012
    Posts:
    1
    Thanks!
     
  32. fixitchris

    fixitchris

    Joined:
    Mar 16, 2011
    Posts:
    21
  33. murugan

    murugan

    Joined:
    Jan 4, 2012
    Posts:
    4
    Your Project Add The Assets ->Plugins->System.data.dll,System.drawing.dll,Mysql.Data.dll
     
  34. JackiM

    JackiM

    Joined:
    Aug 21, 2012
    Posts:
    20
    Using network can we access this sql?
     
  35. arsen

    arsen

    Joined:
    Oct 31, 2011
    Posts:
    5
    zumwalt, Thanks! You my hero!))
     
  36. descenderdante

    descenderdante

    Joined:
    Sep 3, 2009
    Posts:
    218
    Nice contribution...and great job on it..
    But I don''t aprove of this way of doing Unity <-> MySQL because
    1) Limitations to Win and Mac only
    2) Security flaws
     
  37. keithsoulasa

    keithsoulasa

    Joined:
    Feb 15, 2012
    Posts:
    2,126
    I have a very ugly and jerryridged system working on android .

    As long as your using www post it shoudn't make a difference .
    Here's a bit of code to get you started
    NOTE
    IT IS NOT SECURE , YOU HAVE TO ADD YOUR OWN SECURITY MEASURES
    http://wiki.unity3d.com/index.php/Easy_MySQL_Submission
     
  38. Robon

    Robon

    Joined:
    Aug 15, 2012
    Posts:
    9
    On intsalling the packagae I get the following error;
    Assets/MySQLJS.js(52,23): BCE0005: Unknown identifier: 'MySql'.

    Is this package compatible with Unity Indie?
     
  39. Lassiii

    Lassiii

    Joined:
    Aug 10, 2012
    Posts:
    1

    i had the same prob, but i got a solution... go in unity 3d to edit->project setting->player and then choose in other settings net 2.0.....

    but now i got this error:

    edit: my mistake was i has the wrong files, now i got the files from the right folder, now it works
     
    Last edited: Oct 12, 2012
  40. Phiru

    Phiru

    Joined:
    Oct 29, 2012
    Posts:
    32
    Thanks. But I have a problem like blow.(I am uing ver.3.5)

    Assets/MySQLJS.js(52, 23) : Unknown identifier: 'MySql'.

    As I follow you, I created a folder named "Plugins" and added all of .Dll(MySql.Data.dll, System.Data.dll) into "Plugins" folder.

    However, it doesn't still work. It showed same error "unknown identifier".

    It it possible to connect MySql with JS over ver3.5??

    Please let me know that. : (

    Thanks.
     
  41. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Thanks for the plugin. i really appreciate your effort, i'll try in my next Project.
     
  42. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    (bookmarked)
     
  43. Arutha

    Arutha

    Joined:
    Dec 27, 2012
    Posts:
    3
    I have the same problem. Has anyone found a solution?
     
  44. pezz

    pezz

    Joined:
    Apr 29, 2011
    Posts:
    606
    Yes it is possible.
     
  45. twixthehero

    twixthehero

    Joined:
    Jan 17, 2013
    Posts:
    6
    I have found the solution! :D

    Download this: http://www.mysql.com/downloads/connector/net/ and install it.

    Go to C:\Program Files\MySQL\MySQL Connector Net 6.6.4\Assemblies\v2.0 (or Program Files (x86) on 64-bit windows!)

    Replace the MySql.Data.dll with the one in here.

    Problem solved! :D
     
  46. MeditatingGamer

    MeditatingGamer

    Joined:
    Jan 31, 2013
    Posts:
    16
    I keep getting this debuglog. Im not sure whats up since i followed the pdf word for word. The rest of the error is:

    MySql.Data.MySqlClient.MySqlException: The user specified as a definer ('Demo'@'%') does not exist
    at MySql.Data.MySqlClient.MySqlStream.ReadPacket () [0x00000] in <filename unknown>:0
    at MySql.Data.MySqlClient.NativeDriver.GetResult (System.Int32 affectedRow, System.Int32 insertedId) [0x00000] in <filename unknown>:0
    UnityEngine.Debug:Log(Object)
    MySQLCS:ReadEntries() (at Assets/MySQLCS.cs:288)
    MySQLCS:OnGUI() (at Assets/MySQLCS.cs:100)

    Any ideas? Also I connect successfully and can save, its just the loading part...
     
  47. luftballon

    luftballon

    Joined:
    Jan 31, 2013
    Posts:
    1
    Thanks for this plugin!! Works really fine in my application ;)
    One question remains:
    Is there a way to get columns that have the database blob from the database? I've tried it a few times, but every time
    I try to convert the dbReader["blockList"] to an byte[] array or to an arrayList it doesnt seem to work...probably anyone has an idea? :D
     
  48. MeditatingGamer

    MeditatingGamer

    Joined:
    Jan 31, 2013
    Posts:
    16
    Got it to work, even managed to learn a thing or two about MySQL. Now I am running into a different issue. I can get this code to work properly in unity, but once I build my game, nothing seems to work at all, any advice?
     
  49. SevenUnearth

    SevenUnearth

    Joined:
    Jun 26, 2013
    Posts:
    8
    I have exactly the same problem, but I also moved the mysql code into my gameserver.cs. Everything work fine when I start my server into unity but when I build it into a windows application. It's impossible to connect to the database. It's as if MySql was not deploy with the application or something like that.

    If anybody know what is going on...I've been on it the entire day :(
     
  50. SevenUnearth

    SevenUnearth

    Joined:
    Jun 26, 2013
    Posts:
    8
    I found another thread on my last problem, it doesn't work for me yet but it works for other.

    zumwalt:
    "Actually you are mostly right, create a "Plugins" folder, place all the libraries needed for MySQL to work into that folder including the following to make MySQL work (only tested on Windows at the moment, won't have time until this weekend to test on my Mac)

    MySql.Data.dll
    System.Data.dll
    I18N.dll
    I18N.West.dll
    System.Configuration.dll
    System.Drawing.dll
    System.EnterpriseServices.dll
    System.Security.dll

    With those all in your Plugins folder, in theory, it should work. It works right now on my laptop, I haven't bothered to deploy to web build yet, Mac or other devices.
    "

    Thank you zumwalt, I hope it will help others like me.

    http://forum.unity3d.com/threads/65987-MySQL-U3-3-0-0-5f-build-but-can-t-use-MySQL


    Finally figured this out for the 4.1.5 unity version!!!
    All the these dll are in the folder ...\Unity 4.1.5\Editor\Data\MonoBleedingEdge\lib\mono\2.0
    And they must be placed in the server_Data\Managed folder
     
    Last edited: Jul 17, 2013
Thread Status:
Not open for further replies.