SQLite Class - Easier Database Stuff

来源:互联网 发布:数据库如何快速查询 编辑:程序博客网 时间:2024/06/17 06:45
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
Code:  
  1. import          System.Data;  // we import our  data class
  2. import          Mono.Data.SqliteClient; // we import our sqlite client
  3.  
  4. class dbAccess {
  5.     // variables for basic query access
  6.     private var connection : String;
  7.     private var dbcon : IDbConnection;
  8.     private var dbcmd : IDbCommand;
  9.     private var reader : IDataReader;
  10.    
  11.     function OpenDB(p : String){
  12.     connection = "URI=file:" + p; // we set the connection to our database
  13.     dbcon = new SqliteConnection(connection);
  14.     dbcon.Open();
  15.     }
  16.    
  17.     function BasicQuery(q : String, r : boolean){ // run a baic Sqlite query
  18.         dbcmd = dbcon.CreateCommand(); // create empty command
  19.         dbcmd.CommandText = q; // fill the command
  20.         reader = dbcmd.ExecuteReader(); // execute command which returns a reader
  21.         if(r){ // if we want to return the reader
  22.         return reader; // return the reader
  23.         }
  24.     }
  25.    
  26.     function CreateTable(name : String, col : Array, colType : Array){ // Create a table, name, column array, column type array
  27.         var query : String;
  28.         query  = "CREATE TABLE " + name + "(" + col[0] + " " + colType[0];
  29.         for(var i=1; i<col.length; i++){
  30.             query += ", " + col[i] + " " + colType[i];
  31.         }
  32.         query += ")";
  33.         dbcmd = dbcon.CreateCommand(); // create empty command
  34.         dbcmd.CommandText = query; // fill the command
  35.         reader = dbcmd.ExecuteReader(); // execute command which returns a reader
  36.    
  37.     }
  38.    
  39.     function InsertIntoSingle(tableName : String, colName : String, value : String){ // single insert
  40.         var query : String;
  41.         query = "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + ")";
  42.         dbcmd = dbcon.CreateCommand(); // create empty command
  43.         dbcmd.CommandText = query; // fill the command
  44.         reader = dbcmd.ExecuteReader(); // execute command which returns a reader
  45.     }
  46.    
  47.     function InsertIntoSpecific(tableName : String, col : Array, values : Array){ // Specific insert with col and values
  48.         var query : String;
  49.         query = "INSERT INTO " + tableName + "(" + col[0];
  50.         for(var i=1; i<col.length; i++){
  51.             query += ", " + col[i];
  52.         }
  53.         query += ") VALUES (" + values[0];
  54.         for(i=1; i<values.length; i++){
  55.             query += ", " + values[i];
  56.         }
  57.         query += ")";
  58.         dbcmd = dbcon.CreateCommand();
  59.         dbcmd.CommandText = query;
  60.         reader = dbcmd.ExecuteReader();
  61.     }
  62.    
  63.     function InsertInto(tableName : String, values : Array){ // basic Insert with just values
  64.         var query : String;
  65.         query = "INSERT INTO " + tableName + " VALUES (" + values[0];
  66.         for(var i=1; i<values.length; i++){
  67.             query += ", " + values[i];
  68.         }
  69.         query += ")";
  70.         dbcmd = dbcon.CreateCommand();
  71.         dbcmd.CommandText = query;
  72.         reader = dbcmd.ExecuteReader();
  73.     }
  74.    
  75.     function SingleSelectWhere(tableName : String, itemToSelect : String, wCol : String, wPar : String, wValue : String){ // Selects a single Item
  76.         var query : String;
  77.         query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue; 
  78.         dbcmd = dbcon.CreateCommand();
  79.         dbcmd.CommandText = query;
  80.         reader = dbcmd.ExecuteReader();
  81.         var readArray = new Array();
  82.         while(reader.Read()){
  83.             readArray.Push(reader.GetString(0)); // Fill array with all matches
  84.         }
  85.         return readArray; // return matches
  86.     }
  87.  
  88.    
  89.     function CloseDB(){
  90.         reader.Close(); // clean everything up
  91.         reader = null;
  92.         dbcmd.Dispose();
  93.         dbcmd = null;
  94.         dbcon.Close();
  95.         dbcon = null;
  96.     }
  97.    
  98. }
An Example of Creating a Table
You can create two arrays, one being the column headers, and another being the data types.
Code:  
  1. var db : dbAccess;
  2. function Start(){
  3.     db = new dbAccess();
  4.     db.OpenDB("myDB.sqdb");
  5.     var tableName = "myTable";
  6.     var columnNames = new Array("firstName","lastName");
  7.     var columnValues = new Array("text","text");
  8.     db.CreateTable(tableName,columnNames,columnValues);
  9.     db.CloseDB();
  10. }
An Example of Easy Insert
This example is an easy way to add a single record
Code:  
  1. var db : dbAccess;
  2. function Start(){
  3.     db = new dbAccess();
  4.     db.OpenDB("myDB.sqdb");
  5.     var tableName = "myTable";
  6.     // IMPORTANT remember to add single ' to any strings, do not add them to numbers!
  7.     var values = new Array("'Bob'","'Sagat'");
  8.     db.InsertInto(tableName, values);
  9.     db.CloseDB();
  10. }
An Example of Single WHERE select
I am not done with this class, but this is an example of getting an array of items that match a WHERE clause.
Code:  
  1. var db : dbAccess;
  2. function Start(){
  3.     db = new dbAccess();
  4.     db.OpenDB("myDB.sqdb");
  5.     var tableName = "myTable";
  6.     // table name, I want to return everyone whose first name is Bob when their last name is = to Sagat, this returs an array
  7.     var resultArray = db.SingleSelectWhere(tableName, "firstName", "lastName","=","'Sagat'"); // Remember the '' on String values
  8.     print(resultArray[0]);
  9.     // of course you can loop through them all if you wish
  10.     db.CloseDB();
  11.    
  12. }
原创粉丝点击