首先要創建一個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));
}
}
}
}
拖到任意物體上,運行。