Unity3D连接到MySQL错误 [英] Unity3D connection to MySQL error

查看:80
本文介绍了Unity3D连接到MySQL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让Unity客户端纯粹为读取建立与MySQL服务器的连接.建立连接时出现错误.

I'm trying to get Unity client to establish connection to MySQL server purely for reads. I get an error when the connection is being made.

我的代码:

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;
    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);
        connectionString = "Server="+host+";Database="+database+";User ID="+user+";Password="+password+";Pooling=";
        if (pooling){
            connectionString += "true;";
        } else {
            connectionString += "false;";
        }

        try {
            con = new MySqlConnection(connectionString);
            con.Open(); //  THIS is line 47 in the error
            Debug.Log ("Mysql State: " + con.State);
        } catch (Exception e) {
            Debug.Log (e); //  THIS is line 51 in the error
        }

    }
}

错误:

System.Collections.Generic.KeyNotFoundException:给定的密钥不是 出现在字典中.在 System.Collections.Generic.Dictionary`2 [System.String,MySql.Data.MySqlClient.CharacterSet] .get_Item (System.String键)在[0x000a2]中 /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)[0x00000]在:0中 在MySql.Data.MySqlClient.CharSetMap.GetEncoding(DBVersion版本, System.String CharSetName)[0x00000]在:0处 MySql.Data.MySqlClient.Driver.Configure (MySql.Data.MySqlClient.MySqlConnection连接)在[0x00000]中 在MySql.Data.MySqlClient.MySqlConnection.Open处为:0 ()[0x00000] in:0 at(wrapper) 带检查的远程调用) MySql.Data.MySqlClient.MySqlConnection:Open()在 /Users/ThaMacPro/Desktop/Unity中的DatabaseHandler.Awake()[0x0007a] 项目/youtubeMySQL/资产/脚本/DatabaseHandler.cs:47 UnityEngine.Debug:Log(Object)DatabaseHandler:Awake()(在 Assets/Scripts/DatabaseHandler.cs:51)

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.String,MySql.Data.MySqlClient.CharacterSet].get_Item (System.String key) [0x000a2] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150 at MySql.Data.MySqlClient.CharSetMap.GetCharacterSet (DBVersion version, System.String CharSetName) [0x00000] in :0 at MySql.Data.MySqlClient.CharSetMap.GetEncoding (DBVersion version, System.String CharSetName) [0x00000] in :0 at MySql.Data.MySqlClient.Driver.Configure (MySql.Data.MySqlClient.MySqlConnection connection) [0x00000] in :0 at MySql.Data.MySqlClient.MySqlConnection.Open () [0x00000] in :0 at (wrapper remoting-invoke-with-check) MySql.Data.MySqlClient.MySqlConnection:Open () at DatabaseHandler.Awake () [0x0007a] in /Users/ThaMacPro/Desktop/Unity Projects/youtubeMySQL/Assets/Scripts/DatabaseHandler.cs:47 UnityEngine.Debug:Log(Object) DatabaseHandler:Awake() (at Assets/Scripts/DatabaseHandler.cs:51)

谁能告诉我该错误如何解决?

Can anyone tell me how this error can be fixed?

谢谢!

推荐答案

解决方案:必须将字符集添加到connnectionString.

Had to add the charset to the connnectionString.

谢谢@Roberto! :-)

Thank you @Roberto! :-)

这篇关于Unity3D连接到MySQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆