【问题标题】:Query MySQL error查询 MySQL 错误
【发布时间】:2015-12-05 02:15:29
【问题描述】:

我可以打开与 MySQL 5.7 的连接。当我执行查询时,我得到一个错误。

我的代码:

using UnityEngine;
using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using MySql.Data;
using MySql.Data.MySqlClient;

public class DatabaseHandler : MonoBehaviour {

    public string host, database, user, password, charset, soo;
    public bool pooling = true;

    private string connectionString;
    private MySqlConnection con = null;
    private MySqlCommand cmd = null;
    private MySqlDataReader rdr = null;

    private MD5 _md5Hash;

    void Awake() {

        DontDestroyOnLoad(this.gameObject);
        string connectionString = "Server="+host+";Database="+database+";User ID="+user+";Password="+password+";CharSet="+charset+";port=3306"+";Pooling=";
        if (pooling){
            connectionString += "true;";
        } else {
            connectionString += "false;";
        }

        try {
            con = new MySqlConnection(connectionString);
            con.Open();
            Debug.Log ("Mysql State: " + con.State);
            String cmdText = "SELECT * FROM unicorn";
            MySqlCommand cmd = new MySqlCommand(cmdText, con);
            soo = (String) cmd.ExecuteScalar(); //  THIS is line 54 in the error
            Debug.Log (soo);
        } catch (MySqlException ex) 
        {
            Debug.Log("hits ex");
            Console.WriteLine("Error: {0}",  ex.ToString());

        } finally 
        {
            if (rdr != null) 
            {
                rdr.Close();
            }

            if (con != null) 
            {
                con.Close();
            }   
        }
    }

    void OnApplicationQuit() {
        if(con != null) {
            if(con.State.ToString() != "Closed") {
                con.Close();
                Debug.Log ("MySql Connection closed");
            }
            con.Dispose();
        }
    }

    public string GetConnectionState(){
        return con.State.ToString ();
    }
}

错误:

KeyNotFoundException:给定的键不在字典中。 System.Collections.Generic.Dictionary`2[System.String,MySql.Data.MySqlClient.CharacterSet].get_Item (System.String 键)(在 /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) MySql.Data.MySqlClient.CharSetMap.GetCharacterSet(DBVersion 版本, System.String CharSetName) MySql.Data.MySqlClient.MySqlField.SetFieldEncoding () MySql.Data.MySqlClient.MySqlField.set_CharacterSetIndex(Int32 值) MySql.Data.MySqlClient.MySqlField.SetTypeAndFlags(MySqlDbType 类型, ColumnFlags 标志)MySql.Data.MySqlClient.NativeDriver.GetColumnData (MySql.Data.MySqlClient.MySqlField 字段) MySql.Data.MySqlClient.NativeDriver.GetColumnsData (MySql.Data.MySqlClient.MySqlField[] 列) MySql.Data.MySqlClient.Driver.GetColumns(Int32 计数) MySql.Data.MySqlClient.ResultSet.LoadColumns (Int32 numCols) MySql.Data.MySqlClient.ResultSet..ctor (MySql.Data.MySqlClient.Driver d, Int32 statementId, Int32 numCols) MySql.Data.MySqlClient.Driver.NextResult (Int32 statementId) MySql.Data.MySqlClient.MySqlDataReader.NextResult () MySql.Data.MySqlClient.MySqlCommand.ExecuteReader (CommandBehavior 行为)MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar()(包装 远程调用检查) MySql.Data.MySqlClient.MySqlCommand:ExecuteScalar () DatabaseHandler.Awake() (在 Assets/Scripts/DatabaseHandler.cs:54)

附加信息: 在与 MySQL 5.7 服务器建立初始连接时,我遇到了类似的错误。它是通过在 connectionString 中包含字符集来修复的,例如

Database=weather;Server=127.0.0.1;Uid=root;Password=123456;pooling=false;CharSet=utf8;port=3306

我不确定这些信息是否会有所帮助。 如果您好奇,这是我在该问题上的帖子(已解决)。 Unity3D connection to MySQL error

谢谢!

【问题讨论】:

  • 查看第二个最新的方法称为:MySql.Data.MySqlClient.CharSetMap.GetCharacterSet(DBVersion 版本,System.String CharSetName)。您可能传入了无效的字符集
  • 您是否在数据库 int 标识中定义了主键列类型?
  • @Fisher 我没有定义主键
  • @cFrozenDeath 是的,我认为问题可能出在字符集上。不过我不知道该怎么办。
  • 提交错误:bugs.mysql.com

标签: c# mysql unity3d character-encoding


【解决方案1】:

我注意到很多人没有遇到和我一样的问题。所以我最终为我的 MySQL 数据库使用了不同的主机(机架空间)以避免上述问题。通过转到不同的主机,可以避免问题。问题可能与数据库的设置方式有关。

【讨论】:

    【解决方案2】:

    我不知道如何解决这个问题,但是当我们的数据库从羚羊变成梭鱼时,它开始发生在我身上。 BTW varchars 仍然返回,只有当您尝试返回数字数据类型时,您才会收到此错误。至少对我来说!还不知道如何解决它,但希望该信息对某人有所帮助。

    【讨论】:

    • 我可以通过更新 .Net 连接器的版本来解决这个问题
    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 2016-03-29
    • 2014-03-06
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2011-09-02
    相关资源
    最近更新 更多