0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UnityでSQLiteUnityKitを使ってSQLiteを利用する。

Posted at

UnityでSQLiteを利用する

C#の勉強としてゲーム開発をしたときに上手くいかなかった点を、個人的なメモとして残させていただきます。

実行環境・想定は下記。
・Windows 10
・64 ビット オペレーティング システム、x64 ベース プロセッサ
・Unity 2022.3.43f1
・SQLiteUnityKit

基本的には以下の手順

要点を抜粋。

  1. SQLiteUnityKitを取得する
  2. プロジェクトに配置する
    DataTable.cs -> Assets/Scripts
    SqliteDatabase.cs -> Assets/Scripts
  3. DBを作成する
    xxxx.db -> Assets/StreamingAssets ※フォルダなければ作成する。

この状態で、下記も格納しておく必要がある。
libsqlite3.so -> Assets/Plugin/x64 ※フォルダなければ作成する。
(32bitであれば x86に格納する。)

ここまでで利用準備が完了だが、配布されているSQLiteUnityKitはいくつか問題点もあるよう。
Unity SQLiteUnityKit のスクリプト修正・変更のポイント

私は、「マルチバイト文字の対応」のみ修正して利用することとしました。

追加対応

この段階で利用しようとすると、下記のようなエラーが発生した。

.log
DllNotFoundException: sqlite3 assembly:<unknown assembly> type:<unknown type> member:(null)
SqliteDatabase.Open (System.String path) (at Assets/Scripts/SQLite/Common/SqliteDatabase.cs:152)
SqliteDatabase.Open () (at Assets/Scripts/SQLite/Common/SqliteDatabase.cs:143)
SqliteDatabase.ExecuteQuery (System.String query) (at Assets/Scripts/SQLite/Common/SqliteDatabase.cs:218)
SQLite.SystemInfoDao.SelectSystemInfo () (at Assets/Scripts/SQLite/Dao/SystemInfoDao.cs:17)
Home.HomeInitializer.Start () (at Assets/Scripts/Home/HomeInitializer.cs:19)

どうやら、UnityがSQLiteライブラリ(sqlite3.dll)を見つけられないことが原因だと言っている。
「sqlite3.dll」を配置する必要がありそうだった。

(参考)Unity Android/Windows 環境における SQLite の使い方の基本

公式よりダウンロードする。
https://www.sqlite.org/download.html

今回は、64bitなので「sqlite-dll-win64-x64-XXXXXXX.zip」を取得して、解凍後格納する。

sqlite3.dll -> Assets/Plugin/x64
※libsqlite3.so は不要か。

以上で、エラーは解消されて実行することができた。

利用方法

・DB名を"config.db"とした場合、以下の指定方法でSqliteDatabaseに渡す。

    new SqliteDatabase(Application.streamingAssetsPath + "/config.db")

・サンプル

DatabaseConnectionController.cs
using System.IO;
using UnityEngine;

namespace SQLite
{
    public class DatabaseConnectionController
    {
        public string dbPath { get; private set; }
        public SqliteDatabase Conn { get; private set; }

        public DatabaseConnectionController(): this(Application.streamingAssetsPath + "/config.db") { }
        
        public DatabaseConnectionController(string dbPath)
        {
            this.dbPath = dbPath;
        }
        public void CreateConnection()
        {
            if (!File.Exists(dbPath))
            {
                Debug.LogError($"ERROR: Database is not found : {dbPath}");
                return;
            }
            Conn = new SqliteDatabase(this.dbPath);
        }
    }
}
SystemInfoDao.cs
using UnityEngine;

namespace SQLite
{

    public class SystemInfoDao : DatabaseConnectionController
    {
        public string sql { get; private set; }

        public SystemInfoDao()
        {
            sql = "SELECT * FROM system_info Limit 1;";
            this.CreateConnection();
        }
        public SystemInfoMapper SelectSystemInfo()
        {
            return new (Conn.ExecuteQuery(sql));
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?