unity3d有自己对应的sqlite.dll分别需要三个文件
1.Mono.Data.Sqlite.dll
在unity安装文件“Unity\Editor\Data\MonoBleedingEdge\lib\mono”可以找到,注意mono文件夹下面 有对应版本号,可以根据自己的项目来决定选择。
2.System.Data.dll 同上位置一样可以找到
3.sqlite3.dll 就在\Unity\Editor下可以找到
除此之外,还需要把这3个文件放在你的项目的这个路径下面:\Assets\Plugins\,没有 Plugins文件夹就必须创建这个文件夹,然后将这三个dll文件放在该文件夹下面。当然,如果你想能够在PC上面发布成可执行文件,还需要改动一些地 方。在unity3d中的Play Setting ->Other Setting 中将Api Compatibility的等级改为.NET 2.0;那么这些操作做完了以后,如果你的代码写得没有问题,那么你就可以成功了。
注意:下面脚本不要绑定在任何游戏对象身上,大家无需把它当作脚本可以当作一个工具类来使用。
using UnityEngine; using System; using System.Collections; using Mono.Data.Sqlite; public class SqliteDbHelper { private SqliteConnection dbConnection;// 声明一个连接对象 private SqliteCommand dbCommand;// 声明一个操作数据库命令 private SqliteDataReader reader;// 声明一个读取结果集的一个或多个结果流 public SqliteDbHelper(string connectionString)// 数据库的连接字符串,用于建立与特定数据源的连接 { OpenDB(connectionString); } public void OpenDB(string connectionString)//连接数据库 { try { dbConnection = new SqliteConnection(connectionString); dbConnection.Open(); Debug.Log("Connected to db"); } catch (Exception e) { string temp1 = e.ToString(); Debug.Log(temp1); } } public void CloseSqlConnection()// 关闭连接 { if (dbCommand != null) { dbCommand.Dispose(); } dbCommand = null; if (reader != null) { reader.Dispose(); } reader = null; if (dbConnection != null) { dbConnection.Close(); } dbConnection = null; Debug.Log("Disconnected from db."); } public SqliteDataReader ExecuteQuery(string sqlQuery)// 执行查询sqlite语句操作 { dbCommand = dbConnection.CreateCommand(); dbCommand.CommandText = sqlQuery; reader = dbCommand.ExecuteReader(); return reader; } // 查询该表所有数据 public SqliteDataReader ReadFullTable(string tableName) { string query = "SELECT * FROM " + tableName; return ExecuteQuery(query); } // 动态添加表字段到指定表 表名,字段集合 public SqliteDataReader InsertInto(string tableName, string[] values) { string query = "INSERT INTO " + tableName + " VALUES (" + values[0]; for (int i = 1; i < values.Length; ++i) { query += ", " + values[i]; } query += ")"; return ExecuteQuery(query); } // 动态更新表结构 public SqliteDataReader UpdateInto(string tableName, string[] cols, string[] colsvalues, string selectkey, string selectvalue) { //表名,字段集,对于集合值,要查询的字段,要查询的字段值 string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0]; for (int i = 1; i < colsvalues.Length; ++i) { query += ", " + cols[i] + " =" + colsvalues[i]; } query += " WHERE " + selectkey + " = " + selectvalue + " "; return ExecuteQuery(query); } // 动态删除指定表字段数据 public SqliteDataReader Delete(string tableName, string[] cols, string[] colsvalues) { //表名,字段,字段值 string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0]; for (int i = 1; i < colsvalues.Length; ++i) { query += " or " + cols[i] + " = " + colsvalues[i]; } Debug.Log(query); return ExecuteQuery(query); } // 动态添加数据到指定表 public SqliteDataReader InsertIntoSpecific(string tableName, string[] cols, string[] values) { if (cols.Length != values.Length) { throw new SqliteException("columns.Length != values.Length"); } string query = "INSERT INTO " + tableName + "(" + cols[0]; for (int i = 1; i < cols.Length; ++i) { query += ", " + cols[i]; } query += ") VALUES (" + values[0]; for (int i = 1; i < values.Length; ++i) { query += ", " + values[i]; } query += ")"; return ExecuteQuery(query); } // 动态删除表 public SqliteDataReader DeleteContents(string tableName) { string query = "DELETE FROM " + tableName; return ExecuteQuery(query); } // 动态创建表 public SqliteDataReader CreateTable(string name, string[] col, string[] colType) { //表名,字段,字段值 if (col.Length != colType.Length) { throw new SqliteException("columns.Length != colType.Length"); } string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0]; for (int i = 1; i < col.Length; ++i) { query += ", " + col[i] + " " + colType[i]; } query += ")"; return ExecuteQuery(query); } // 根据查询条件 动态查询数据信息 public SqliteDataReader SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values) { //表名,查询数据集合,字段,操作,值 if (col.Length != operation.Length || operation.Length != values.Length) { throw new SqliteException("col.Length != operation.Length != values.Length"); } string query = "SELECT " + items[0]; for (int i = 1; i < items.Length; ++i) { query += ", " + items[i]; } query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' "; for (int i = 1; i < col.Length; ++i) { query += " AND " + col[i] + operation[i] + "'" + values[0] + "' "; } return ExecuteQuery(query); } }
创建本地数据库,创建C#脚本SqliteDbTest.cs直接绑定在摄像机中。
using UnityEngine; using System.Collections; using Mono.Data.Sqlite; public class SqliteDbTest : MonoBehaviour { void Start() { //创建数据库名称为jiangshuai.db SqliteDbHelper db = new SqliteDbHelper("data source=jiangshuai.db"); //创建数据库表,与字段 db.CreateTable("biao", new string[] { "name", "qq", "email", "blog" }, new string[] { "text", "text", "text", "text" }); //在数据库中连续插入三条数据 db.InsertInto("biao", new string[] { "'姜帅'", "'123456789'", "'123456789@qq.com'", "'www.jiangshuai.com'" }); db.InsertInto("biao", new string[] { "'傅克诏'", "'223456789'", "'223456789@qq.com'", "'www.fukezhao.com'" }); db.InsertInto("biao", new string[] { "'孟涛'", "'323456789'", "'323456789@qq.com'", "'www.mengtao.com'" }); //然后在删掉两条数据 //db.Delete("biao", new string[] { "qq", "qq" }, new string[] { "'123456789'", "'223456789'" }); //动态查询数据信息 SqliteDataReader sqReader = db.SelectWhere("biao", new string[] { "name", "email" }, new string[] { "qq" }, new string[] { "=" }, new string[] { "123456789" }); while (sqReader.Read()) //表名,查询数据集合,字段,操作,值 { Debug.Log(sqReader.GetString(sqReader.GetOrdinal("name")) + sqReader.GetString(sqReader.GetOrdinal("email"))); } //删除表 //db.DeleteContents("biao"); //关闭对象 db.CloseSqlConnection(); } }
运行游戏后,数据库对象会自动生成在项目的根目录中。查看数据库的软件使用的是SQLiteExpertpers。如下图所示,数据库文件jiangshuai.db已经生成在项目的根目录中,接着使用SQLiteExpertpers软件将这个数据库打开。数据库的表名为biao,打开表后字段包含name、 qq 、email、 blog。都是我们在代码中创建的。
如下图所示,我使用Log也将数据库name 与 email的字段打印了出来。最后强调一点,我们在OnStart方法中db.CreateTable创建数据库表,如果重复创建系统会抛出错误。避免这个情况请保证你的数据库表只会被创建一次。