使用untiy也快两年了。突然没事做。所以就想总结点什么。我在网络上看到得大部分u3d实现本地数据得方面都是使用sqlite得方案。其实这也可以想像。我最早也是用拖入dll得方式来实现得。但是后面我其实有些时候我得插件不一定能支持得了2.0得环境。
因为untiy其实在很多方面会使用到第三方插件。像igui,ngui,prime31等。
所以当我遇到了这个问题后。我就想看看有没有比较好得解决方案。当然啦。我也不是什么牛人。但是我用百度谷歌还是很牛得,哈哈。然后就是搜索了很多得网站.不管是国外的还是国内得。也看了很多得案例。皇天不负有心人。最后还是在国内的一个博客上找到了一个文章。太久了。所以也忘记是那个了。在这里实在有些对不住原作者。我再这里只是做一个搬运工得工作。当然啦。这个我还是自己用过得。我想在用的就是自己在这个基础上封装得工具类。挺好得。只是在错误提示方面可能不能很明确得显示给我们。这点我也没办法。
如果有什么更好得方案。希望能发给我一份。大家共同进步嘛。相互交流。在这里先谢谢了。
下面是源码(我在作者得基础上添加了一些东西。但是这个同时也给了我一个启示。有时候我们可以从最原始得源头去解决一些问题。):
public class AEsqlite { public enum sqliteState{ OK = 0,/* Successful result */ ERROR = 1,/* sql error or missing database */ INTERNAL = 2,/* Internal logic error in sqlite */ PERM = 3,/* Access permission denied */ ABORT = 4,/* Callback routine requested an abort */ BUSY = 5,/* The database file is locked */ LOCKED = 6,/* A table in the database is locked */ NOMEM = 7,/* A malloc() Failed */ READONLY = 8,/* Attempt to write a readonly database */ INTERRUPT = 9,/* Operation terminated by sqlite3_interrupt()*/ IOERR = 10,/* Some kind of disk I/O error occurred */ CORRUPT = 11,/* The database disk image is malformed */ NOTFOUND = 12,/* Unknown opcode in sqlite3_file_control() */ FULL = 13,/* Insertion Failed because database is full */ CANTOPEN = 14,/* Unable to open the database file */ PROTOCOL = 15,/* Database lock protocol error */ EMPTY = 16,/* Database is empty */ SCHEMA = 17,/* The database schema changed */ TOOBIG = 18,/* String or BLOB exceeds size limit */ CONSTRAINT = 19,/* Abort due to constraint violation */ MISMATCH = 20,/* Data type mismatch */ MISUSE = 21,/* Library used incorrectly */ NOLFS = 22,/* Uses OS features not supported on host */ AUTH = 23,/* Authorization denied */ FORMAT = 24,/* Auxiliary database format error */ RANGE = 25,/* 2nd parameter to sqlite3_bind out of range */ NOTADB = 26,/* File opened that is not a database file */ ROW = 100,/* sqlite3_step() has another row ready */ DONE = 101,/* sqlite3_step() has finished executing */ } /* [DllImport("__Internal")] private static extern int aear_genTracker(string detectorFile,string trackerFile); */ /// /// 打开数据库连接,此方法与sqlite3_open()对应 /// /// /// 数据库文件的路径 /// /// 打开数据连接成功时,返回数据库连接对象;否则返回IntPtr.Zero。 /// /// 当返回的Intptr为IntPtr.Zero时,表明打开数据库连接失败。 /// 当使用完此方法返回的数据库连接对象时,需要调用AEsqlite.Close方法关闭数据库连接。 /// public static IntPtr Open(string dbFile){ IntPtr pDb= IntPtr.Zero; sqlite3_open(dbFile,ref pDb); return pDb; } /// /// Compiling An sql command. 与sqlite3_prepare_v2()对应 /// /// /// sql Statement /// /// /// 数据库连接对象 /// /// /// 编译sql statment成功时,返回statment对象;否则返回IntPtr.Zero。 /// /// /// 当返回IntPtr.Zero时,表明编译sql statement失败; ///当使用完此方法返回sql statement对象时,需要调用AEsqlite.FinalizeStmt()释放sql statement对象 /// public static IntPtr Prepare(string sqlStatement,IntPtr pDb){ IntPtr pStmt= IntPtr.Zero; IntPtr pzTail = IntPtr.Zero; sqlite3_prepare_v2(pDb,sqlStatement,-1,ref pStmt,ref pzTail) ; return pStmt; } /// /// Step the specified pStmt. /// /// /// P statement. public static sqliteState Step(IntPtr pStmt){ return (sqliteState)sqlite3_step(pStmt); } /// /// 释放sql statement对象占用的资源 /// /// /// /// /// /// 由AEsqlite.Prepare()所创建的sql statement对象 public static bool FinalizeStmt(IntPtr pStmt){ sqlite3_finalize(pStmt); return true; } /// /// 关闭数据库连接 /// /// /// 由AEsqlite.Open()所创建的数据库连接对象 public static bool Close(IntPtr pDb){ sqlite3_close(pDb); return true; } /// /// 返回最近操作sqlite3数据库时,所产生的错误 /// /// /// 数据库连接对象 /// /// 返回的字符串为UNICODE编码格式 public static string Errmsg(IntPtr pDb){ IntPtr strPtr = sqlite3_errmsg16(pDb); return Marshal.PtrToStringUni(strPtr); } public static int ColumnInt(IntPtr pStmt,int iCol){ return sqlite3_column_int(pStmt,iCol); } public static double ColumnDouble(IntPtr pStmt,int iCol){ return sqlite3_column_double(pStmt,iCol); } /// 返回的字符串为UNICODE编码格式 public static string ColumnText(IntPtr pStmt,int iCol){ IntPtr strPtr = sqlite3_column_text16(pStmt,iCol); return Marshal.PtrToStringUni(strPtr); } public static long LastInsertRowId(IntPtr pDb){ return sqlite3_last_insert_rowid(pDb); } public static UnityEngine.Vector2 sqlite3GetTable(IntPtr pDb,string cmd ) { int nRow = 0; int Column = 0; string errmsg = ""; IntPtr dbResult = IntPtr.Zero; Debuger.Log ((sqliteState)sqlite3_get_table(pDb,cmd,ref dbResult,ref nRow,ref Column,ref errmsg)); return new UnityEngine.Vector2(Column,nRow); } public static IntPtr sqlite3FreeTable( IntPtr dbResult ) { return sqlite3_free_table(dbResult); } [DllImport("sqlite3.dll")] private static extern int sqlite3_open(string filename,ref IntPtr ppDb); [DllImport("sqlite3.dll")] private static extern int sqlite3_prepare_v2(IntPtr pDb,string sqlText,int nByte,ref IntPtr ppStmt,ref IntPtr pzTail); [DllImport("sqlite3.dll")] private static extern int sqlite3_step(IntPtr pStmt); [DllImport("sqlite3.dll")] private extern static int sqlite3_finalize(IntPtr pStmt); [DllImport("sqlite3.dll")] private extern static int sqlite3_close(IntPtr pDb); [DllImport("sqlite3.dll")] private extern static int sqlite3_column_int(IntPtr pStmt,int iCol); [DllImport("sqlite3.dll")] private extern static double sqlite3_column_double(IntPtr pStmt,int iCol); [DllImport("sqlite3.dll")] private extern static IntPtr sqlite3_column_text16(IntPtr pStmt,int iCol); //返回的字符串为UTF-16编码格式 [DllImport("sqlite3.dll")] private extern static IntPtr sqlite3_errmsg16(IntPtr pDb); //返回的字符串为UTF-16编码格式 [DllImport("sqlite3.dll")] private extern static long sqlite3_last_insert_rowid(IntPtr pDb); [DllImport("sqlite3.dll")] private extern static IntPtr sqlite3_get_table(IntPtr pDb,string cmd,ref IntPtr dbResult,ref int nRow,ref int Column,ref string errmsg); [DllImport("sqlite3.dll")] private extern static IntPtr sqlite3_free_table( IntPtr dbResult ); }原文链接:https://www.f2er.com/sqlite/201112.html