加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite Class - Easier Database Stuff

发布时间:2020-12-12 20:29:42 所属栏目:百科 来源:网络整理
导读: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 da
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. class dbAccess {
  4. // variables for basic query access
  5. private var connection : String;
  6. var dbcon : IDbConnection;
  7. var dbcmd : IDbCommand;
  8. var reader : IDataReader;
  9. function OpenDB (p : String ) connection = "URI=file:" + p; // we set the connection to our database
  10. dbcon = new SqliteConnection (connection );
  11. dbcon. Open ( }
  12. function BasicQuery (q : String,r : boolean { // run a baic Sqlite query
  13. dbcmd = dbcon. CreateCommand ); // create empty command
  14. dbcmd. CommandText = q; // fill the command
  15. reader = dbcmd. ExecuteReader // execute command which returns a reader
  16. if (r // if we want to return the reader
  17. return reader; // return the reader
  18. function CreateTable ( name : Array,colType : Array // Create a table,name,column array,column type array
  19. var query : query = "CREATE TABLE " + name + "(" + col [ 0 ] + " " + colType ];
  20. for var i= 1; i<col. length; i++ query += "," + col [i query += ")";
  21. CommandText = query; function InsertIntoSingle (tableName : : // single insert
  22. "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + function InsertIntoSpecific ") VALUES (" + values (i= 1; i<values. + values CommandText = query;
  23. function InsertInto // basic Insert with just values
  24. " VALUES (" + values function SingleSelectWhere // Selects a single Item
  25. "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue;
  26. var readArray = new while (reader. Read readArray. Push (reader. GetString // Fill array with all matches
  27. return readArray; // return matches
  28. function CloseDB reader. Close // clean everything up
  29. reader = null;
  30. dbcmd. Dispose dbcmd = dbcon. dbcon = }
An Example of Creating a Table
You can create two arrays,one being the column headers,and another being the data types.
Code: var db : dbAccess;
  • function Start db = new dbAccess db. OpenDB ( "myDB.sqdb" var tableName = "myTable";
  • var columnNames = "firstName","lastName" var columnValues = "text",21)">"text" CreateTable (tableName,columnNames,columnValues CloseDB }
  • An Example of Easy Insert
    This example is an easy way to add a single record
    Code: // IMPORTANT remember to add single ' to any strings,do not add them to numbers!
  • var values = "'Bob'",21)">"'Sagat'" InsertInto }
  • 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: // 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 "lastName",21)">"=",0)">// Remember the '' on String values
  • print (resultArray ] // of course you can loop through them all if you wish
  • }
  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读