0
問題:以下はすべてUnity Editorでうまく動作しますが、Android用にビルドするとデータベース接続は完了せず、例外メッセージに「sqlite3」と表示されます。Android用のUnityプロジェクトでSQLiteが動作しない
詳細:
私が使用しているプラグインはAsset Storeの上で発見された(SQLiter )
The database isn't created automatically when the connection
is created and when I trid debug through asking if the file
exists on a certain path, the file doesn't exist. This "Path"
is the the Database location (URI).
私は他の人にURIを試してみましたが、機能したことがありません...
"URI=file:" + Application.persistentDataPath + "/StreamingAssets/" + SQL_DB_NAME + ".db";
"URI=file:" + Application.persistentDataPath + "!/assets/" + SQL_DB_NAME + ".db";
"URI=file:" + Application.persistentDataPath + "/" + SQL_DB_NAME + ".db";
"URI=file:" + Application.persistentDataPath + "/StreamingAssets/" + SQL_DB_NAME + ".db";
"URI=file:" + SQL_DB_NAME + ".db";
...
using UnityEngine;
using UnityEngine.UI;
using System.Data;
using System;
using Mono.Data.SqliteClient;
using System.IO;
using System.Text;
namespace SQLiter
{
public class DB : MonoBehaviour
{
public static DB Instance = null;
public bool DebugMode = false;
public Text debug, debug2;
private static string _sqlDBLocation = "";
private const string SQL_DB_NAME = "TestDB";
private const string SQL_TABLE_NAME = "Things";
private const string COL_NAME = "name";
private const string COL_DESCRIPTION = "description";
private IDbConnection _connection = null;
private IDbCommand _command = null;
private IDataReader _reader = null;
private string _sqlString;
public bool _createNewTable = false;
void Awake()
{
if (DebugMode)
Debug.Log("--- Awake ---");
_sqlDBLocation = "URI=file:" + Application.persistentDataPath + "!/assets/" + SQL_DB_NAME + ".db";
Debug.Log(_sqlDBLocation);
Instance = this;
SQLiteInit();
}
void Start()
{
if (DebugMode)
Debug.Log("--- Start ---");
// Invoke("Test", 3);
}
private void SQLiteInit()
{
Debug.Log("SQLiter - Opening SQLite Connection at " + _sqlDBLocation);
_connection = new SqliteConnection(_sqlDBLocation);
_command = _connection.CreateCommand();
debug.text = File.Exists(_sqlDBLocation).ToString();
try {
_connection.Open();
} catch (System.Exception e) {
// debug.text = e.Message;
}
_command.CommandText = "PRAGMA journal_mode = WAL;";
_command.ExecuteNonQuery();
_command.CommandText = "PRAGMA journal_mode";
_reader = _command.ExecuteReader();
if (DebugMode && _reader.Read())
Debug.Log("SQLiter - WAL value is: " + _reader.GetString(0));
_reader.Close();
_command.CommandText = "PRAGMA synchronous = OFF";
_command.ExecuteNonQuery();
_command.CommandText = "PRAGMA synchronous";
_reader = _command.ExecuteReader();
if (DebugMode && _reader.Read())
Debug.Log("SQLiter - synchronous value is: " + _reader.GetInt32(0));
_reader.Close();
_command.CommandText = "SELECT name FROM sqlite_master WHERE name='" + SQL_TABLE_NAME + "'";
_reader = _command.ExecuteReader();
if (!_reader.Read())
{
Debug.Log("SQLiter - Could not find SQLite table " + SQL_TABLE_NAME);
_createNewTable = true;
}
_reader.Close();
if (_createNewTable)
{
Debug.Log("SQLiter - Dropping old SQLite table if Exists: " + SQL_TABLE_NAME);
_command.CommandText = "DROP TABLE IF EXISTS " + SQL_TABLE_NAME;
_command.ExecuteNonQuery();
Debug.Log("SQLiter - Creating new SQLite table: " + SQL_TABLE_NAME);
_sqlString = "CREATE TABLE IF NOT EXISTS " + SQL_TABLE_NAME + " (" +
COL_NAME + " TEXT UNIQUE, " +
COL_DESCRIPTION + " TEXT)";
_command.CommandText = _sqlString;
_command.ExecuteNonQuery();
}
else
{
if (DebugMode)
Debug.Log("SQLiter - SQLite table " + SQL_TABLE_NAME + " was found");
}
_connection.Close();
}
おそらくこれが必要とされていませんが、は...アンドロイドで使用Application.persistentDataPath
ため
public void Insert(string name, string description)
{
name = name.ToLower();
_sqlString = "INSERT INTO " + SQL_TABLE_NAME
+ " ("
+ COL_NAME + ","
+ COL_DESCRIPTION
+ ") VALUES ("
+ "'" + name + "',"
+ "'" + description + "');";
if (DebugMode)
Debug.Log(_sqlString);
ExecuteNonQuery(_sqlString);
}
public void ExecuteNonQuery(string commandText)
{
_connection.Open();
_command.CommandText = commandText;
_command.ExecuteNonQuery();
_connection.Close();
}
private void SQLiteClose()
{
if (_reader != null && !_reader.IsClosed)
_reader.Close();
_reader = null;
if (_command != null)
_command.Dispose();
_command = null;
if (_connection != null && _connection.State != ConnectionState.Closed)
_connection.Close();
_connection = null;
}
public string GetAllWords()
{
StringBuilder sb = new StringBuilder();
debug.text = "get-antes";
_connection.Open();
debug.text = "get-depois";
_command.CommandText = "SELECT * FROM " + SQL_TABLE_NAME;
_reader = _command.ExecuteReader();
while (_reader.Read())
{
sb.Append(_reader.GetString(0)).Append("\n");
sb.Append(_reader.GetString(1)).Append("\n");
sb.AppendLine();
if (DebugMode)
Debug.Log(sb.ToString());
}
_reader.Close();
_connection.Close();
return sb.ToString();
}
void OnDestroy()
{
SQLiteClose();
}
}
}
は既に権限を持っていますが、動作しません...他のURIを試しましたが動作しません...メインの投稿は新しいURIで編集されました... – Evxs
このスレッドに相談してくださいhttps://forum.unity3d .com/threads/unity-3d-android-sqlite-examples.114660 / –