1.iBoxDB 簡介
http://www.iboxdb.com/
2.選擇原因
Sqlite3不支持WebGL (選擇方案:WebGL平臺用iboxDB,其它平臺用Sqlite3)
iboxDB 支持:JAVA C# Android Unity Xamarin Mono Nashorn Linux Windows
安裝簡單,Unity只需要一個dll即可。
Web需要主動調用持久化(例如5分鐘調用一次保存,或者當某個頁面關閉時保存等等,根據開發者自定義,如果不保存,刷新瀏覽器 iboxdb 將沒有保存數據)。
Application.ExternalEval("FS.syncfs(false, function (err) {});"); //持久化函數
3.簡單用法
-
注意變量必須用 ?替代,防止sql注入。
Select("from Email where category == ? & type == ? order by created_at desc, id desc",1,1);
-
字段數據類型,必須一致。
Class{
int a;
long b;
}
Select("from Email where a == ? & b == ?",1,(long)1); //需要傳long型
- mysql in 語句。這個語法數據庫不支持,可以翻譯成條件判斷。
mysql :where category = 1 AND type in (1,2,3,4) order by create_at desc, id desc limit 0, 10
iBoxDb :
var types = new int[]{1,2,3,4};
可以寫個轉化函數:var Sql = from Email where category == ? & MessageTypesStrings(type.Length) order by create_at desc, id desc limit 0, 10
轉化後就是: from Email where category == ? & (type == ? | type == ? | type == ? | type == ?) order by create_at desc, id desc limit 0, 10
private string MessageTypesToStrings(int length){
var result = "";
if (length == 0)
return result;
if (length > 1) {
for (var i = 0; i < length - 1; i++) {
result += "type==?" + "|";
}
}
result += "type==?";
return "(" + result + ")";
}
多參數合併問題:
Select(Sql,1,types); 數組這麼傳參不對,可以轉化成 5個參數。
Select(Sql,ConvertParams(1,types)); //[1,1,2,3,4]
private object[] ConvertParams(params object[] parameters){
var list = new List<object> ();
foreach (var item in parameters) {
if (item.GetType () == typeof(int[])) {
var items = item as int[];
foreach (var item1 in items) {
list.Add ((int)item1);
}
} else if (item.GetType () == typeof(long[])) {
var items = item as long[];
foreach (var item1 in items) {
list.Add ((long)item1);
}
} else {
list.Add (item);
}
}
return list.ToArray ();
}
- group by 語法不支持
自己取出數據,然後用linq語句進行篩選。
var sums = from n in QueryData
group n by new{n.category,n.type} into g
select new {Total = g.Count ()};
from n in QueryData
group n by new {n.category,n.type}
into g
select g.OrderByDescending (t => t.id).ThenByDescending (t => t.created_at).FirstOrDefault ()
大多數運算 都可以Select DataBase 然後進行 linq邏輯操作。
- 邏輯操作符
Select("from Email where (type == ?) & (reward_time > ? | life_time-?<=?)",1,1,2,1); //支持運算
4.我的helper類
public class iBoxDBHelper {
private static iBoxDBHelper _ins;
public static iBoxDBHelper ins{
get{
if (null == _ins) {
_ins = new iBoxDBHelper ();
}
return _ins;
}
}
public static string boxDBEmail = "Email";
private string DBBoxDir{
get{
return Application.persistentDataPath;
}
}
private AutoBox m_autoBox = null;
private DB m_db;
public AutoBox Box{
get{
return m_autoBox;
}
}
public void InitDB(){
CreateDirectory ();
if (null == m_autoBox) {
DB.Root (DBBoxDir);
m_db = new DB (3);//3=自定義數字,在這沒有具體意義。
m_db.GetConfig ().EnsureTable<DBEmail> (boxDBEmail, "id", "category");
m_db.GetConfig ().EnsureIndex<DBEmail> (boxDBEmail, "created_at", "read_at");
m_autoBox = m_db.Open();
}
}
public void ChangeDB(){
m_db.Dispose ();
m_autoBox = null;
InitDB ();
}
private void CreateDirectory () {
if(!Directory.Exists (DBBoxDir)) {
Directory.CreateDirectory (DBBoxDir);
}
}
public bool Insert(string tableName, DBEmail data){
return _ins.m_autoBox.Insert (tableName, data);
}
public void InsertMulti(string tableName, List<DBEmail> data){
using(var box = _ins.m_autoBox.Cube())
{
Binder binder = box.Bind(tableName);
foreach (var item in data) {
binder.Insert (item);
}
box.Commit ();
}
}
public void Update(string tableName, object data){
_ins.m_autoBox.Update (tableName, data);
}
public void UpdateMulti(string tableName, List<object> data){
using(var box = _ins.m_autoBox.Cube())
{
Binder binder = box.Bind(tableName);
foreach (var item in data) {
binder.Update (item);
}
box.Commit();
}
}
public DBEmail GetOne(string sql,params object[] param){
var dbEmail = new DBEmail ();
var datas = _ins.m_autoBox.Select <DBEmail> (sql, param);
foreach (var item in datas) {
dbEmail = item;
}
return dbEmail;
}
public void Delete(string tableName,string QL){
_ins.m_autoBox.Delete (tableName, QL);
}
}