【Unity】sqlite数据库在Unity中的使用
【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数组添加数组元素的时候需要使用new object[]{"'Sivan'",99}
3.SqliteDataReader使用之后需要关闭!
4.数据库使用完之后必须关闭!
各平台下数据库存储的绝对路径
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