简体   繁体   English

Unity Android MySQL连接功能失败:索引超出了数组的范围

[英]Unity Android MySQL connection function failing: Index was outside the bounds of the array

I have a class to handle all of the database interactions between the client and database (MySQL) (I know and understand that I shouldn't allow direct access to a database) 我有一个类来处理客户端和数据库(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);
    }
}

This works flawlessly in the editor. 这在编辑器中可以完美地工作。 When building and running on Android I get this error message via Monitor: 在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.

I'm not using an array to store the contents of the reader, here is the line that calls this example (it happens for all database interactions, not just this one, and not just this database querying function): 我没有使用数组来存储阅读器的内容,这是调用此示例的行(它发生在所有数据库交互中,而不仅是这个,也不只是这个数据库查询功能):

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

Here's the contents of tbl_Store: 这是tbl_Store的内容:

(Item_ID (int)), (Item_Name (varchar)), (Item_Price (float)), (In_Use (bit)), (DT_Stamp (datetime)) (Item_ID(int)),(Item_Name(varchar)),(Item_Price(float)),(使用中(位)),(DT_Stamp(日期时间))

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 

I also tried it in a much more basic project to get the core error message returned by Android: 我还在一个更基础的项目中进行了尝试,以获取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

I'm probably missing something really simple here but any help would be greatly appreciated! 我可能在这里错过了一些非常简单的东西,但是任何帮助将不胜感激!

I found the answer: 我找到了答案:

In order to get Android device to communicate externally you need to add I18N.dll and I18N.West.dll to your assets, this was the difference between it working in the Unity Editor and the Android APK. 为了使Android设备能够与外部进行通信,您需要将I18N.dll和I18N.West.dll添加到资产中,这是它在Unity Editor和Android APK中工作之间的区别。

There's nothing named that way ( MAX(DT_Stamp) ) in the result set. 结果集中没有这样的名称( MAX(DT_Stamp) )。 I would use AS name in your projection clause and reference it that way. 我会在您的投影子句中使用AS name ,并以此方式进行引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM