Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

How do I store a date using MySQL?

Discussion in 'General Discussion' started by FVelasco, Nov 6, 2019.

  1. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    I want to save a date in a server and I think that using MySQL is the way, but I don't really know how to apply it. I have this code:

    Code (CSharp):
    1. public DateTime fechaHoy = System.DateTime.Today;
    2. public DateTime fecha = new DateTime(2019, 12, 24, 0, 0, 0);
    3. public RawImage evento;
    4.  
    5. void Start()
    6. {
    7.   if(fechaHoy == fecha)
    8.   {
    9.   evento.gameObject.SetActive(true);
    10.   }
    11. }
    When it's the day of "fecha", an image is shown. What I want to do is to change the date from a server and send it to the client (the DateTime in the code is just an example, I have to change it to get the value from the server). What's the easiest way to do this? Thank you.
     
    Last edited: Nov 7, 2019
  2. LukeDawn

    LukeDawn

    Joined:
    Nov 10, 2016
    Posts:
    398
    You'll be needing a MySQL library or DB connection library; and remember that calls will be asynchronous.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,824
    I'm not sure what you're planning. Databases don't "send" anything to clients. They respond to requests. If you want something to update this database you'll want a server build of some kind, though doing a simple update to a date field in a database could be made a simple as a shell script called by a cron job.

    Clients could request from the database through a PHP web server interface, or if you went with a server side Unity build the clients could connect to that server using most any of the available network API's. Though if you went that route, storing a single date in a database is a bit overkill since you could just write it to playerprefs on the server.
     
    Antypodish and Ryiah like this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,230
    @FVelasco besides you need setup server with SQL and PHP, you need use web request from Unity.
    This is not only solution, but in most cases sufficient.
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,155
    Why a database?

    You could trivially easily have a text file with a date in it on a website. Use the WWW class to fetch the text file and read it. Job's done.

    Databases are useful if you have lots of data, or if data contains relationships, or if there are rules to enforce, or other complex use cases. If all you need to do is share some values there are much easier ways.
     
  6. LukeDawn

    LukeDawn

    Joined:
    Nov 10, 2016
    Posts:
    398
    I started off using databases, but moved to keeping all characters and world objects as data files as I had no need for data relationships. The difference in speed and resource usage was ridiculously large.
     
  7. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Never talk directly to a SQL server from a client. Use a rest service. The database needs to be hidden behind firewalls. There are a ton of reasons why you shouldn't directly let the clients talk to the database, from security to versioning.
     
    Ryiah likes this.
  8. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    Me: Select star from potato table.

    *Waits*

    Me now screaming: Select star from potato table!!

    *Mumbles* Still nothing. [client looks at me funny]

    Client: Maybe you need to use a rest service?

    Me: Sure, *kicks off shoes, lies back on couch and requests a Frappacino* . . . I'll try again in five minutes!
     
    Last edited: Nov 8, 2019
    AndersMalmgren likes this.
  9. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    Yeah you are right, that seems simpler. I only want to change the date remotely and the program has to read and use the values, so I'm going to search how to do what you have said.
     
  10. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    It seems that the WWW class is obsolete.
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,155
    I'm sure a quick search will reveal its replacement. ;)
     
    FVelasco and Antypodish like this.