源码分享 收藏本版 已有3人收藏 +发表新主题
查看: 7984|回复: 0
打印 上一主题 下一主题

[分享] Unity使用JavaScript访问SQLite本地数据库

[复制链接]

[分享] Unity使用JavaScript访问SQLite本地数据库

yanjianghao 发表于 2016-5-5 10:18:20 浏览:  7984 回复:  0 只看该作者 复制链接
  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.     }
复制代码

使用方法:
1. 创建本地数据库
  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.     }
复制代码

2.插入数据
  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.     }
复制代码

3.使用WHERE select
  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.     }
复制代码
分享至:
| 人收藏
回复

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright © 2013-2017 ARinChina-增强现实中国技术论坛   All Rights Reserved.