1. 程式人生 > >【Unity】sqlite資料庫在Unity中的使用

【Unity】sqlite資料庫在Unity中的使用

匯入

mono.data.sqlite.dll
System.data.dll
sqlite3.dll

Assets/Plugins資料夾

【Windows電腦】
mono.data.sqlite.dllSystem.data.dll在Unity\Editor\Data\MonoBleedingEdge\lib\mono\2.0

window電腦下載:Precompiled Binaries for Windows(注意32位和64位)

下載完之後開啟壓縮包將sqlite.dll匯入到Assets/Plugins資料夾下

【Mac電腦】

mono.data.sqlite.dllSystem.data.dll

在/Applications/Unity/Unity.app(右鍵顯示包內容)/Contents/MonoBleedingEdge/lib/mono

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}

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