iBoxDB—Unity WebGL Sqlite3替代方案

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);
	}
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
通用软件自动更新模块,本程序不能主动运行,需要传递命令行参数,格式如下: 命令行参数 两种工作模式: 1.详细信息升级,格式如下(路径尽量使用""引起来,避免因为路径包含空格而调用失败): 自动升级模块可执行路径 需要升级的主程序路径 旧版本号 新版本号 程序下载地址 detail(固定写法) AutoUpdate.exe "E:\Visual Studio 2005\Projects\AutoUpdate\debug\测试.exe" 旧版本号 新版本号 http://.../WlwDir/10000/TaobaoDB.exe detail 2.主动分析模式,格式如下: 自动升级模块可执行路径 需要升级的主程序路径 旧版本号 新版本号(占位符,没实际用处,为了和模式1具有相同数量的参数,方便处理) 包含程序升级信息的网址(返回Json数据) linkurl(固定写法) AutoUpdate.exe "E:\Visual Studio 2005\Projects\AutoUpdate\debug\E语言示例.exe" 3.0 1.0 http://localhost:9572/UpdateWeb/Default.aspx linkurl http://localhost:9572/UpdateWeb/Default.aspx 返回数据为: {"SoftName":"测试软件","NewVersion":"2.0","DownUrl":"下载软件的url链接"} 如果返回的NewVersion比当前软件的版本高,则执行升级,否则升级程序自动退出。 注意: 上述格式最后还有一个可选的参数,为待升级程序的主窗口句柄,如果不知道怎么回事,此参数可以忽略。 VC写法如下,sprintf(参数,"0x%x",GetSafeHwnd()) //使用Shell启动升级程序即可 ShellExecute(this->m_hWnd,"open","AutoUpdate.exe",命令行参数,NULL,SW_SHOW); 有不明白的朋友,请与我联系
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值