【问题标题】:Unity Android MySQL connection function failing: Index was outside the bounds of the arrayUnity Android MySQL连接功能失败:索引超出数组范围
【发布时间】:2019-01-01 19:02:56
【问题描述】:

我有一个类来处理客户端和数据库 (MySQL) 之间的所有数据库交互(我知道并理解我不应该允许直接访问数据库)

public T getDatabaseValue<T>(string value, string query)
{
    try
    {
        connection = new MySqlConnection(connectionString);
        connection.Open();
        command = new MySqlCommand(query, connection);
        MySqlDataReader reader = command.ExecuteReader();
        if (reader.Read())
        {
            return (T)reader[value];
        }
        else
            return default(T);
    }
    catch (MySqlException SQLex)
    {
        Debug.Log("Database get failed (SQL Exception), query: " + query);
        Debug.Log("Exception Message: " + SQLex.Message);   
        return default(T);
    }
    catch (System.Exception ex)
    {
        Debug.Log("Database get failed (System Exception), query: " + query);
        Debug.Log("Exception Message: " + ex.Message);
        return default(T);
    }
}

这在编辑器中完美运行。在 Android 上构建和运行时,我通过 Monitor 收到此错误消息:

01-01 15:21:14.141: I/Unity(23761): (Filename: ./Runtime/Export/Debug.bindings.h Line: 43)
01-01 15:21:14.829: I/Unity(23761): Database get failed (System Exception), query: SELECT MAX(DT_Stamp) FROM tbl_Store;
01-01 15:21:14.829: I/Unity(23761):  
01-01 15:21:14.829: I/Unity(23761): (Filename: ./Runtime/Export/Debug.bindings.h Line: 43)
01-01 15:21:14.829: I/Unity(23761): Exception Message: Index was outside the bounds of the array.

我没有使用数组来存储阅读器的内容,这是调用此示例的行(它发生在所有数据库交互中,不仅仅是这个,而且不仅仅是这个数据库查询函数):

    DateTime DB_Date = getDatabaseValue<DateTime>("MAX(DT_Stamp)", "SELECT MAX(DT_Stamp) FROM tbl_Store;");

这是 tbl_Store 的内容:

(Item_ID (int)), (Item_Name (varchar)), (Item_Price (float)), (In_Use (bit)), (DT_Stamp (datetime))

4 Hero Upgrade #1 100 1 2018-12-11 09:53:14 
5 Hero Upgrade #2 300 1 2018-12-11 09:53:14 
6 Hero Upgrade #3 700 1 2018-12-11 09:53:14 

我还在一个更基本的项目中尝试过,以获取 Android 返回的核心错误消息:

01-01 17:23:48.307: E/Unity(10476): IndexOutOfRangeException: Index was outside the bounds of the array. 
01-01 17:23:48.307: E/Unity(10476): at System.Diagnostics.TraceInternal.get_AppName () [0x0000e] in <ac210d81537245bc838518cc8e845861>:0 
01-01 17:23:48.307: E/Unity(10476): at System.Diagnostics.TraceInternal.TraceEvent (System.Diagnostics.TraceEventType eventType, System.Int32 id, System.String format, System.Object[] args) [0x0003d] in <ac210d81537245bc838518cc8e845861>:0 
01-01 17:23:48.307: E/Unity(10476): at System.Diagnostics.Trace.TraceError (System.String message) [0x00000] in <ac210d81537245bc838518cc8e845861>:0 
01-01 17:23:48.307: E/Unity(10476): at MySql.Data.MySqlClient.MySqlTrace.LogError (System.Int32 id, System.String msg) [0x00028] in <326e9aab93854e739606c3572c385a34>:0 
01-01 17:23:48.307: E/Unity(10476): at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () [0x00031] in <326e9aab93854e739606c3572c385a34>:0 
01-01 17:23:48.307: E/Unity(10476): at MySql.Data.MySqlClient.MySqlPool.GetConnection () [0x0001c] in <326e9aab93854e739606c3572c385a34>:0 
01-01 17:23:48.307: E/Unity(10476): at MySql.Data.MySqlClient.MySqlConnection.Open () [0x000f3] in <326e9aab93854e739606c3572c385a34>:0 
01-01 17:23:48.307: E/Unity(10476): at texttest1.Start () [0x00011] in <dd75ac8e3f854a97ac132dae6ec658fd>:0

我可能在这里遗漏了一些非常简单的东西,但我们将不胜感激!

【问题讨论】:

  • getDatabaseValue&lt;DateTime&gt;("dt_stamp", "SELECT MAX(DT_Stamp) dt_stamp FROM tbl_Store;") 也会失败吗?
  • 嘿,是的,所以我用于“更简单”版本的代码如下:` void Start () { connection = new MySqlConnection(connectionString);连接.打开(); command = new MySqlCommand("SELECT Item_Name FROM tbl_Store;", connection); MySqlDataReader reader = command.ExecuteReader(); text = GameObject.Find("Text").GetComponent();阅读器.Read(); text.text = reader["Item_Name"].ToString(); }`
  • 实际上并没有使用相同的查询,它仍然在 Monitor.bat 中产生相同的结果

标签: c# mysql .net visual-studio unity3d


【解决方案1】:

我找到了答案:

为了让 Android 设备与外部通信,您需要将 I18N.dll 和 I18N.West.dll 添加到您的资产中,这是它在 Unity 编辑器和 Android APK 中工作的区别。

【讨论】:

    【解决方案2】:

    结果集中没有以这种方式命名的内容 (MAX(DT_Stamp))。我会在您的投影子句中使用AS name 并以这种方式引用它。

    【讨论】:

    • 您好,感谢您的快速回复!我明白你的意思,但是 SQL 语句的返回值通过 MySQL 数据库返回一个名称属性为“MAX(DT_Stamp)”的值。
    • 就像我说的它在统一客户端中工作得很好,它只是在 Android 上失败
    • @JamesCoyle 这可能只是特定 mysql ado.net 提供商的问题。
    • 确实如此,但如果是这样的话,它肯定不会在 Unity 编辑器中工作吗?
    • @JamesCoyle 谁知道。其中许多可能会有所不同,因为驱动其中一些的是非托管代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多