【发布时间】: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