using iBoxDB.LocalServer; using System; using System.IO; namespace Highway.Assemble.common { public class BoxDB : IBoxDB { public BoxDB() { CreatDB(); } public AutoBox auto = null; private void CreatDB() { if (auto == null) { string dbpath = System.Environment.CurrentDirectory + "\\DB"; if (!Directory.Exists(dbpath)) { Directory.CreateDirectory(dbpath); } DB.Root(dbpath); DB db = new DB(1); //load from Resources //db = new DB(((TextAsset)(UnityEngine.Resources.Load("db2"))).bytes); // two tables(Players,Items) and their keys(ID,Name) db.GetConfig().EnsureTable("MesSoft", "index"); db.GetConfig().EnsureTable("Equip", "index"); db.GetConfig().EnsureTable("Sensor", "index"); db.GetConfig().EnsureTable("Collect", "index"); { // [Optional] // if device has small memory & disk db.MinConfig(); // smaller DB file size db.GetConfig().DBConfig.FileIncSize = 1; } auto = db.Open(); } } public AutoBox GetAutoBox() { if (auto == null) { // Debug.LogWarning("请先创建数据库"); return null; } return auto; } /// /// 添加数据 索引自增 /// /// /// 表名 /// 数据 public void Insert(string table, T data) where T : BaseObject { data.index = auto.Id(1);//索引自增 data.time = System.DateTime.Now;// CatLib.SystemTime.Timestamp(System.DateTime.Now); auto.Insert(table, data); } //foreach (PlayerInfo item in auto.Select("from PlayerInfo Age ==?",15)) /// /// 删除数据库,IBoxDB中没有直接删除一个表的接口,所以这个方法显得格外亲切~ /// 注意:删除数据库之前要关闭该数据库 /// /// 数据库地址 public void DeleteDataBase(int address) { iBoxDB.DBDebug.DDebug.DeleteDBFiles(address); } } public interface IBoxDB { void Insert(string table, T data) where T : BaseObject; AutoBox GetAutoBox(); } public class BaseObject { public int index; public DateTime time;//=DateTime.Now;//long public BaseObject() { } } public static class IDHelper { // helper long -> int if using int ID public static int Id(this AutoBox auto, byte pos, int step = 1) { return (int)auto.NewId(pos, step); } } }