Hello, I thought I would share my basic Sqlite class for those who are interesting in learning how to use unity with it or those who are looking to implement something similar.
This will help create cleaner code for those who need to use databases.
Ultimately I want to create an editor tool for database so that we can have another tool on top of Unity's awesome-ness.
"Update - 8/14 I added a Create Table Function"
"Update - 8/14 I added a Insert Functions"
The Class
An Example of Creating a TableCode:
import System.Data; // we import our data class import Mono.Data.SqliteClient; // we import our sqlite client class dbAccess { // variables for basic query access private var connection : String; private var dbcon : IDbConnection; private var dbcmd : IDbCommand; private var reader : IDataReader; function OpenDB(p : String){ connection = "URI=file:" + p; // we set the connection to our database dbcon = new SqliteConnection(connection); } function BasicQuery(q : String, r : boolean){ // run a baic Sqlite query dbcmd = dbcon.CreateCommand(); // create empty command dbcmd.CommandText = q; // fill the command reader = dbcmd.ExecuteReader(); // execute command which returns a reader if(r){ // if we want to return the reader return reader; // return the reader } } var query : String; query += ", " + col[i] + " " + colType[i]; } query += ")"; dbcmd = dbcon.CreateCommand(); // create empty command dbcmd.CommandText = query; // fill the command reader = dbcmd.ExecuteReader(); // execute command which returns a reader } var query : String; dbcmd = dbcon.CreateCommand(); // create empty command dbcmd.CommandText = query; // fill the command reader = dbcmd.ExecuteReader(); // execute command which returns a reader } var query : String; query = "INSERT INTO " + tableName + "(" + col[0]; query += ", " + col[i]; } query += ") VALUES (" + values[0]; query += ", " + values[i]; } query += ")"; dbcmd = dbcon.CreateCommand(); dbcmd.CommandText = query; reader = dbcmd.ExecuteReader(); } var query : String; query = "INSERT INTO " + tableName + " VALUES (" + values[0]; query += ", " + values[i]; } query += ")"; dbcmd = dbcon.CreateCommand(); dbcmd.CommandText = query; reader = dbcmd.ExecuteReader(); } function SingleSelectWhere(tableName : String, itemToSelect : String, wCol : String, wPar : String, wValue : String){ // Selects a single Item var query : String; query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue; dbcmd = dbcon.CreateCommand(); dbcmd.CommandText = query; reader = dbcmd.ExecuteReader(); while(reader.Read()){ } return readArray; // return matches } function CloseDB(){ reader = null; dbcmd = null; dbcon = null; } }
You can create two arrays, one being the column headers, and another being the data types.
An Example of Easy InsertCode:
This example is an easy way to add a single record
An Example of Single WHERE selectCode:
I am not done with this class, but this is an example of getting an array of items that match a WHERE clause.
Code:
var db : dbAccess; db = new dbAccess(); db.OpenDB("myDB.sqdb"); var tableName = "myTable"; // table name, I want to return everyone whose first name is Bob when their last name is = to Sagat, this returs an array var resultArray = db.SingleSelectWhere(tableName, "firstName", "lastName","=","'Sagat'"); // Remember the '' on String values // of course you can loop through them all if you wish db.CloseDB(); }

Reply With Quote