以前やったけどもっと簡単な例
参考: http://forum.unity3d.com/threads/89658-C-SQLite-example-for-Unity-3-3
using UnityEngine; using System; using Mono.Data.Sqlite; public class dbController : MonoBehaviour { protected SqliteConnection dbconn; void Awake () { if (Application.platform == RuntimePlatform.IPhonePlayer) { dbconn = new SqliteConnection ("URI=file:" + Application.dataPath + "/../Data.db"); } else { dbconn = new SqliteConnection ("URI=file:" + Application.dataPath + "/Data.db"); } dbconn.Open (); SqliteCommand cmd = null; try { cmd = new SqliteCommand ("SELECT firstname, lastname FROM adressbook", dbconn); SqliteDataReader reader = cmd.ExecuteReader (); if (reader.HasRows) { while (reader.Read()) { string FirstName = reader.GetString (0); string LastName = reader.GetString (1); Debug.Log (FirstName + LastName); } } reader.Close (); reader = null; } catch { Debug.Log ("Error reading DB"); } cmd.Dispose (); cmd = null; dbconn.Close (); dbconn = null; } }
Data.dbをAsset配下に入れる。
sqlite3 Data.db
CREATE TABLE adressbook ( firstname string, lastname string );
insert into adressbook values('yamada', 'taro'); insert into adressbook values('nakamura', 'hanako');
Mono.Data.Sqlite.dllをどっかから拾ってくる。
ちなみにこれだと、iOSでうまく開かないかも?
あと、パラメータの渡し方が苦労する
これ参考にする。
http://forum.unity3d.com/threads/7866-Unity-and-Sqlite/page3
SQLite-1.0.66.0-managedonly-binaries.zip
これがいいのか?
Mono.Data.Sqlite.dllとは違う。クラス名はSqliteがSQLiteになる。
こちらは
「URI=file:」でも「Data Source="」でも行けそう。