Hi everyone!, I was looking for a tutorial, who can make me learn SQL on UNITY, so, everyone just scary me!
Thanks for Tempest, and Xandeck, but i want something more, and more simple, so, i will share with you guys what i’ve learn, I’m from Brasil, so, my english is from video games! ,
Here we are going to learn:
- Connecting Unity to a Database, with login and Password
- Making a Search using Select.
- Have results from the database to use.
- Use what we have got.
Starting:
First, you are going to have a database running on a server, (i don’t intend to explain this, cause i don’t know, i’ve got a server from my company to test, later i’ll explain, when i learn someday, maybe), but, with a server running, create a new Unity Project, name it whatever you like, and don’t import anything, because it’s faster this way , and then, create a single Javascript file, rename it to whatever you like, and let’s start the cool stuff!
The Headers:
Type it there!
import System;
import System.Data;
import System.Data.SqlClient;
Yes! We are using C# libs in Javascript!, we are sooo bad
Observations:
And now, create a Start function to test the code.
function Start () {
[INDENT]//and don't belive i've teaching this! sorry for making you feel like a noob![/INDENT]
}
The Harder part:
And now, ladies and gentlemans, the connection comand!
lets create the main class object of the connection:
var dbcon : IDbConnection;
you can name dbcon to DATABASE, to be more intuitive.
and lets do the connection:
var connectionString : String =
"Server=xxx.xxx.xxx.xxx;" + // put the ip here!
"Database=INSERT DATABASE NAME HERE;" +
"User ID=INSERT LOGIN HERE;" +
"Password=INSERT PASSWORD HERE;";
dbcon = new SqlConnection(connectionString);
dbcon.Open();
We created a var to store the “url” to the bank, and Instantiate the class for connections.
Remenber: we are using SQL Server, (in my case, SQL Server 2008 R2), but i’m sure it can work with SQlite, Oracle, or something else, all you need to do for it to work, its learn,
You can: search for a code that have connection to the base you want, and just look for the Connection String.
And now, we are going to make a SQL command, the classic, the one, the awesome, SELECT!
//create the class EXECUTIONER, that execute SQL commands
var dbcmd : IDbCommand = dbcon .CreateCommand();
//string var, to save the command we want to use
var cmdSql : String = SELECT [COLOR="blue"]idUsuario[/COLOR], [COLOR="blue"]nmUsuario[/COLOR], [COLOR="blue"]dsLogin[/COLOR], [COLOR="blue"]dsSenha [/COLOR]FROM [COLOR="#ff8c00"]FestaJunina[/COLOR].[COLOR="red"]tbUsuario[/COLOR];
//we add the command, as string, to the executor, to shot it!
dbcmd.CommandText = cmdSql;
//and then, we create a table, like a normal db table, to use it on unity, and we use the function that "plays" the command
var reader : IDataReader = dbcmd.ExecuteReader();
Subtitle of colors:
Blue: Columns Names.
Orange: Main Database name.
Red: Table name.
this all that we made till here, did:
- Conected to the database
- Send a command
- Execute a command
- Returned results
Now, lets read what we got from the database!
while(reader .Read()) {
var id : String = reader ["[COLOR="#2e8b57"]idUsuario[/COLOR]"].ToString();
var nome : String = reader ["[COLOR="#2e8b57"]nmUsuario[/COLOR]"].ToString();
var login : String = reader ["[COLOR="#2e8b57"]dsLogin[/COLOR]"].ToString();
var senha : String = reader ["[COLOR="#2e8b57"]dsSenha[/COLOR]"].ToString();
print ("ID: " + id + "NAME: " + nome + "LOGIN: " + login + "PASSWORD: " + senha);
}
This 'll make the following thing:
For every while loop, the table we created to store the table that is the result of our query on the database, the reader jumps to the next line.
Did you get it? No?
hahahahahaha, man! i have to go to a english school! Humpf
Ok, "reader " is the name of my table, that holds the data i’ve got from my search. ok?
The reader could have n lines, and have 4 columns, and they are: id, name, login, password.
we need to go on every line, to read it, column by column. (it’s column right? or Colunm? or, whatever…)
Green: Name that i’ve used to created the table.
and, after that, use:
reader .Close();
reader = null;
dbcon.Close();
dbcon = null;
I now you got what this code means.
This is it!
In my unity it have returned:
ID: 1 NAME: Daniel LOGIN: daniel@XxXxXx.com.br PASSWORD: 123456
UnityEngine.MonoBehaviour:print(Object)
ConectaSQLSERVER:Start() (at Assets/ConectaSQLSERVER.js:23)
Hope you got it!
Thanks everyone, any questions, put it in here.
I’ll show later how to create a database, because, i have to get back to work :o my boss its close!
And God bless you all!
because He gives me wisdom for that.