SampleSQLiteUnityKit
Unityにおけるデータベースを操作するためのプログラム
Description
UnityにおいてSQLiteUnityKitを使用したデータベースを操作するためのデモプログラム
Requirement
-
SQLiteのバイナリ
-
動作確認環境
- Unity 5.4.1.f1
- SQLite
Usage
使い方
Install
-
データベースを作成するために、SQLiteのツールをダウンロードする ここ
展開し、展開したディレクトリでcmdを開く 以下のコマンドを打つ(データベースを作成している)
C:\sqlite-tools-win32-x86-3150200> sqlite3 config.db sqlite> create table user(id integer, name text, old integer, address text); sqlite> .exit
-
SQLiteUnityKitをクローンまたはダウンロードする ここ
-
SQLiteのバイナリをダウンロードする windows32bitの場合:https://sqlite.org/2016/sqlite-dll-win32-x86-3150200.zip windows64bitの場合:https://sqlite.org/2016/sqlite-dll-win64-x64-3150200.zip
-
Unityを開く
-
Assetsに各種ダウンロードしたファイル以下のように入れる Assets\Plugin\Android\libsqlite3.so (32bitの場合)Assets\Plugin\x86\sqlite3.dll (64bitの場合)Assets\Plugin\x64\sqlite3.dll Assets\Script\DataTable.cs Assets\Script\SqliteDatabase.cs
-
データベースを操作するためのスクリプトファイルを作成する ここではDatabase.csとする
using UnityEngine; using System.Collections; public class Database : MonoBehaviour { // Use this for initialization void Start () { // Insert SqliteDatabase sqlDB = new SqliteDatabase("config.db"); string query = "insert into user values(1, 'hoge', 20, 'america')"; sqlDB.ExecuteNonQuery(query); // Select string selectQuery = "select * from user"; DataTable dataTable = sqlDB.ExecuteQuery(selectQuery); string name = ""; foreach(DataRow dr in dataTable.Rows){ name = (string)dr["name"]; Debug.Log ("name:" + name); } } // Update is called once per frame void Update () { } }
-
GameObjectを作成し、作成したスクリプトをAddComponentする
参考文献
- http://ameblo.jp/mk-soundtrack/entry-12086698310.html
- http://qiita.com/hiroyuki7/items/5335e391c9ed397aee50
- http://pro132007.wp.xdomain.jp/unitysqliteを使う-その1/
Licence
This software is released under the MIT License, see LICENSE.