2017-04-21 116 views
0

问题:下面的所有内容在Unity编辑器上都能正常工作,但是当我为Android构建时,与数据库的连接无法完成,异常消息显示为“sqlite3”。SQLite不适用于Android的Unity项目

详情:

,我使用被发现在资源商店(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(); 
    } 

} 

}

回答

0

在Android上使用Application.persistentDataPath,您需要更改构建设置为Android。更改配置>将访问权限写入外部(SD卡)。

+0

已经有权限,它不起作用...我尝试了其他URI但不起作用...主帖子被编辑了新的URI ... – Evxs

+0

请参阅此主题https://forum.unity3d .COM /线程/团结-3D-Android的源码,examples.114660 / –

相关问题