导入
mono.data.sqlite.dll
System.data.dll
sqlite3.dll
到Assets/Plugins文件夹
【Windows电脑】
mono.data.sqlite.dll和System.data.dll在Unity\Editor\Data\MonoBleedingEdge\lib\mono\2.0
sqlite3.dll需要下载:https://www.sqlite.org/download.html
window电脑下载:Precompiled Binaries for Windows(注意32位和64位)
下载完之后打开压缩包将sqlite.dll导入到Assets/Plugins文件夹下
【Mac电脑】
mono.data.sqlite.dll和System.data.dll在/Applications/Unity/Unity.app(右键显示包内容)/Contents/MonoBleedingEdge/lib/mono
sqlite3.dll需要下载:https://www.sqlite.org/download.html
Mac电脑下载:Precompiled Binaries for Mac OS X (x86)
下载完之后打开压缩包将sqlite.dll导入到Assets/Plugins文件夹下
简单的封装了一个sqlite的工具类,其中很多地方都没有完善,希望和大家共同学习和进步,同学们完善之后也希望贡献一下代码
using UnityEngine; using System.Collections; using Mono.Data.sqlite; public class sqlite { public sqliteConnection connection; private sqliteCommand command; public sqlite(string path) { connection = new sqliteConnection (path); // 创建sqlite对象的同时,创建sqliteConnection对象 connection.Open (); // 打开数据库链接 Command(); } public sqliteCommand Command() { command = connection.CreateCommand (); return command; } // 【增加数据】 public sqliteDataReader InsertData(string table_name,string [] fieldNames,object [] values) { // 如果字段的个数,和数据的个数不相等,无法执行插入的语句,所以返回一个null if (fieldNames.Length != values.Length) { return null; } command.CommandText = "insert into " + table_name + "("; for (int i = 0; i < fieldNames.Length; i++) { command.CommandText += fieldNames[i]; if (i < fieldNames.Length-1) { command.CommandText += ","; } } command.CommandText += ")" + "values ("; for (int i = 0; i < values.Length; i++) { command.CommandText += values [i]; if (i < values.Length - 1) { command.CommandText += ","; } } command.CommandText += ")"; Debug.Log (command.CommandText); return command.ExecuteReader (); } // 【删除数据】 public sqliteDataReader DeleteData(string table_name,string [] conditions) { command.CommandText = "delete from " + table_name + " where " + conditions [0]; for (int i = 1; i < conditions.Length; i++) { // or:表示或者 // and:表示并且 command.CommandText += " or "+ conditions[i]; } return command.ExecuteReader (); } // 【修改数据】 public sqliteDataReader UpdateData(string table_name,string []values,string [] conditions) { command.CommandText = "update " + table_name + " set " + values [0]; for (int i = 1; i < values.Length; i++) { command.CommandText += "," + values [i]; } command.CommandText += " where " + conditions[0]; for (int i = 1; i < conditions.Length; i++) { command.CommandText += " or " + conditions [i]; } return command.ExecuteReader (); } // 【查询数据】 public sqliteDataReader SelectData(string table_name,string [] fields) { command.CommandText = "select " + fields [0]; for (int i = 1; i < fields.Length; i++) { command.CommandText += "," + fields [i]; } command.CommandText += " from " + table_name; return command.ExecuteReader (); } // 【查询整张表的数据】 public sqliteDataReader SelectFullTableData(string table_name) { command.CommandText = "select * from " + table_name; return command.ExecuteReader (); } // 【关闭数据库】 public void CloseDataBase() { connection.Close (); command.Cancel (); } }
简单的使用一下封装的代码
using UnityEngine; using System.Collections; using Mono.Data.sqlite; public class Test : MonoBehavIoUr { // Use this for initialization void Start () { // 数据库文件的具体路径,有的是.sqlite/.db string path = "data source =" + Application.streamingAssetsPath + "/" + "database0117.sqlite"; sqlite sql = new sqlite (path); sqliteDataReader reader1 = sql.InsertData ("FirstTable",new string[]{ "name","score" },new object[]{"'Sivan'",99}); // 读取到的信息使用之后需要关闭 reader1.Close (); sqliteDataReader reader2 = sql.DeleteData ("FirstTable",new string[]{"name = 'LONG'" }); reader2.Close (); sql.CloseDataBase (); } // Update is called once per frame void Update () { } }
有几个需要注意的地方
1.封装的每一个语句的方法返回值都是sqliteDataReader(执行命令的方法有三个,封装的时候选择了返回内容最多的一个)
2.参数是字符串的时候,通过object数组添加数组元素的时候需要使用newobject[]{"'Sivan'",99}
各平台下数据库存储的绝对路径
PC:"data source=" + Application.streamingAssetsPath + "/dataBase.db";
Mac:"data source=" + Application.streamingAssetsPath + "/dataBase.db"; Android:"URI=file:" + Application.persistentDataPath + "/dataBase.db"; iOS:"data source=" + Application.persistentDataPath + "/dataBase.db"; 数据库文件可以是.db或者.sqlite