我可以打开一个连接到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`2System.String,MySql.Data.MySqlClient.CharacterSet.get_Item (System.String key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) MySql.Data.MySqlClient.CharSetMap.GetCharacterSet (DBVersion version,System.String CharSetName) MySql.Data.MySqlClient.MySqlField.SetFieldEncoding () MySql.Data.MySqlClient.MySqlField.set_CharacterSetIndex (Int32 value) MySql.Data.MySqlClient.MySqlField.SetTypeAndFlags (MySqlDbType type ),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,( MySql.Data.MySqlClient.MySqlCommand.ExecuteReader (CommandBehavior behavior) MySql.Data.MySqlClient.MySqlCommand.ExecuteReader () MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar () )(包装器远程处理-调用-检查) MySql.Data.MySqlClient.MySqlCommand:ExecuteScalar () DatabaseHandler.Awake () (在资产/脚本/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
谢谢!
发布于 2015-12-10 20:28:20
我注意到很多人的问题和我不一样。因此,为了避免上述问题,我为我的MySQL数据库使用了一个不同的主机(rackspace)。通过到另一个主机,问题避免了。问题可能在于如何设置数据库。
发布于 2017-06-19 20:48:28
我不知道这是怎么解决的,但当我们的分贝从羚羊变成梭鱼时,它就开始发生在我身上了。顺便说一下,只有当您尝试返回数字数据类型时,才会得到此错误。至少对我来说!还不知道如何解决这个问题,但希望这个信息能帮助到别人。
https://stackoverflow.com/questions/34100495
复制相似问题