Unity 3D + mySQL no working (taken from the example page)

I have a minimalistic code for Unity + mySQL taked from the example pdf. But is not working. I don’t know if Im doing something wrong or if the example is wrong.

Here’s my code:

var constr = "Server=localhost;Database=demo;User ID=root;Password=;Pooling=true";
var con;
var query;

function Awake () {
	try
	{
	// setup the connection element
	con = new MySql.Data.MySqlClient.MySqlConnection(constr);
	// lets see if we can open the connection
	con.Open();
	Debug.Log("Connection State: " + con.State);
	}
	catch (ex)
	{
	Debug.Log(ex.ToString());
	}

}

function OnApplicationQuit()
{
	Debug.Log("killing con");
	if (con != null)
	{
		con.Close();
		con.Dispose();
	}
}

function Start() { Insertar(); }

function Insertar() {
	query = "INSERT INTO tablademo (nombre,clave) VALUES ('david','miclave')";
	if (con.State.ToString() != "Open")
	con.Open();
	
	
	var cmd = new MySql.Data.MySqlClient.MySqlCommand(query, con);
	cmd.ExecuteNonQuery();
	

}

EDIT: The error is NullReferenceException: Object reference not set to an instance of an object and it points to the line if (con.State.ToString() != “Open”). I think that con object never was created in the right way.

Finally, as I read is reccomended, I put a php file between Unity and the database. And I access to it like this:

var w = WWW("http://mypathtofile");
yield w;

It works perfectly and I got more control of the data that the client sends! :slight_smile: