簡體   English   中英

SQLite在Android的Unity項目上不起​​作用

[英]SQLite doesn't work on Unity project for Android

問題:下面的所有內容在Unity Editor上都可以正常運行,但是當我為Android構建時,與數據庫的連接無法完成,並且異常消息顯示為“ sqlite3”。

細節:

我正在使用的插件在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();
        }

也許這不是必需的,但是...

    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();
    }

}

}

要在Android中使用Application.persistentDataPath ,您需要更改android的構建設置。 更改配置>寫入外部(SD卡)的權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM