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
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Trouble connecting app with MySQL Database

Discussion in 'Editor & General Support' started by Silloty, Aug 14, 2019.

  1. Silloty

    Silloty

    Joined:
    Jan 8, 2018
    Posts:
    21
    Hi everyone

    I'm trying to connect my app to a MySQL database but I'm encountering an issue.
    While in Visual Studio 2017, everything is fine, it goes wrong when I'm trying to build my app.
    Indeed, I have the following errors :

    Assets\Scripts\DatabaseManager.cs(29,18): error CS0012: The type 'DbConnection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.



    I've tried to remove manually the reference and add it again, but it didn't work.
    I also tried to replace the reference from the one located here : C:\Program Files\Unity 2018\Unity\Editor\Data\Mono\lib\mono\unity\System.Data.dll, but nothing changes.
    Does anyone have an idea how can I fix it ?
    I'm using Unity 2018.3.12f1

    Thanks a lot
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    7,159
    Please show your code
     
  3. Silloty

    Silloty

    Joined:
    Jan 8, 2018
    Posts:
    21
    Hello JeffDUnity3D

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.Data;
    6. using System.Data.Common;
    7. using MySql.Data;
    8. using MySql.Data.MySqlClient;
    9.  
    10. public class DatabaseManager : MonoBehaviour
    11. {
    12.     MySqlConnection conn;
    13.     string connStr;
    14.     int i = 1;
    15.  
    16.     string[] arrayOfSpeed;
    17.     string[] globalArray;
    18.  
    19.     public TextMesh SpeedText;
    20.     public TextMesh TemperatureText;
    21.     public TextMesh VibrationText;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         connStr = "server=192.168.20.30;port=3306;database=externalSensor;user=admin;password=secret; default command timeout=60";
    27.         conn = new MySqlConnection(connStr);
    28.         Debug.Log(connStr);
    29.         try
    30.         {
    31.             Debug.Log("Connecting to MySQL");
    32.             conn.Open();
    33.             Debug.Log("Success !");
    34.          
    35.            
    36.         }
    37.         catch (System.Exception)
    38.         {
    39.             Debug.Log("error");
    40.             throw;
    41.         }
    42.     }
    43.  
    44.  
    45.     void FixedUpdate()
    46.     {
    47.         string sqlRequest = "SELECT id, dataString from sensorBDD where id = " + i + ";";
    48.         MySqlCommand cmd = new MySqlCommand(sqlRequest, conn);
    49.         MySqlDataReader rdr = cmd.ExecuteReader();
    50.         while (rdr.Read())
    51.         {
    52.             globalArray = rdr[1].ToString().Split('/');
    53.            
    54.             SpeedText.text = "Speed : " +  globalArray[2] + " km/h";
    55.             TemperatureText.text = " Temperature : " + globalArray[3] + " °C";
    56.             VibrationText.text = "Vibration values : " + globalArray[4];
    57.  
    58.         }
    59.         rdr.Close();
    60.         i++;
    61.     }
    62. }

    I don't have any trouble in the code. This is working perfectly fine in play mode.
    The only point what is not working is when I'm trying to build, I'm getting the error I've mentionned first.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    7,159
    Yeah, I was just looking for the "using.." statements which you have included. So you know, and an entirely separate issue, that IP address is only visible on your home network, it's an internal address and no one else would be able to connect. Typically you host such a server IP address on AWS or on Google Cloud or Microsoft Azure. And generally you never send direct SQL commands from a client on a single connection. Generally you use a web service that in turn connects to your database, and exposes a proper object model.
     
    Joe-Censored likes this.
  5. Silloty

    Silloty

    Joined:
    Jan 8, 2018
    Posts:
    21
    I know it. My app is only running on a local network and it's still a prototype.
    But do you know why the error is occuring in that case ?
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    7,159
    You typically want to use SQLite on a device.
     
  7. Silloty

    Silloty

    Joined:
    Jan 8, 2018
    Posts:
    21
    I've read that SQLite isn't a network database.
    Anyway, I managed to solve my problem by integrating a PHP API and using UnityWebRequest.

    Thanks for your time
     
unityunity