標籤:
首先要建立一個sqlite的資料庫,記住檔案地址,拷貝到Assets目錄下,建立的資料庫檔案尾碼為.sqlite。具體建立方法百度sqlite
然後百度Mono.Data.Sqlite,這是一個dll檔案,下載下來,同樣需要放到Assets目錄下
接下來建立c#指令碼,
using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.IO;
public class SQLiteDemo : MonoBehaviour {
string sqlQuery = "create table unity (unity1 int,unity2 int)";
string sqlQuery = "select * from unity";
// Use this for initialization
void Start () {
string connectionString = "Data Source = " + Application.dataPath + "/test.sqlite";
SqliteConnection dbConnection;
dbConnection = new SqliteConnection (connectionString);
dbConnection.Open ();
SqliteCommand dbCommand;
dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
dbCommand.ExecuteNonQuery ();
SqliteDataReader dbReader;
dbReader = dbCommand.ExecuteReader ();
while(dbReader.Read())
{
for(int i = 0; i< dbReader.FieldCount; i++)
{
Debug.Log(dbReader.GetName(i));
Debug.Log(dbReader.GetValue(i));
}
}
}
}
拖到任意物體上,運行。
Unity串連本機資料庫sqlite