###前提・実現したいこと
UnityからAWSのMySQLデータベースへ接続し、データの取得や更新、追加等の処理を行う
###発生している問題・エラーメッセージ
MySQLへ接続できない
KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.String,System.Object].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) MySql.Data.MySqlClient.MySqlConnectionStringBuilder.get_Database () MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString (System.String value) MySql.Data.MySqlClient.MySqlConnection..ctor (System.String connectionString) (wrapper remoting-invoke-with-check) MySql.Data.MySqlClient.MySqlConnection:.ctor (string) MySQLTest+<CreateData>c__Iterator0.MoveNext () (at Assets/Scripts/MySQLTest.cs:36) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) MySQLTest:Start() (at Assets/Scripts/MySQLTest.cs:20)
###該当のソースコード
C#
1using UnityEngine; 2using System; 3using System.Collections; 4 5using MySql.Data; 6using MySql.Data.MySqlClient; 7 8public class MySQLTest : MonoBehaviour 9{ 10 11 string SERVER = "DBインスタンスのエンドポイント"; 12 string DATABASE = "DB名"; 13 string USERID = "ユーザ名"; 14 string PORT = "3306"; 15 string PASSWORD = "DBインスタンス作成時に設定したパスワード"; 16 string TABLENAME = "hoge"; 17 18 void Start() 19 { 20 StartCoroutine(CreateData()); 21 } 22 23 IEnumerator CreateData() 24 { 25 MySqlConnection con = null; 26 27 string conCmd = 28 "server=" + SERVER + ";" + 29 "database=" + DATABASE + ";" + 30 "userid=" + USERID + ";" + 31 "port=" + PORT + ";" + 32 "password=" + PASSWORD; 33 34 try 35 { 36 con = new MySqlConnection(conCmd); 37 con.Open(); 38 Debug.Log("success"); 39 } 40 catch (MySqlException ex) 41 { 42 Debug.Log(ex.ToString()); 43 } 44 45 string createCmd = "CREATE TABLE " + TABLENAME + "(ID INT(3),NAME VARCHAR(10))"; 46 47 MySqlCommand cmd = new MySqlCommand(createCmd, con); 48 49 IAsyncResult iAsync = cmd.BeginExecuteReader(); 50 51 while (!iAsync.IsCompleted) 52 { 53 yield return 0; 54 } 55 56 con.Close(); 57 con.Dispose(); 58 } 59}
###その他の説明
コードはこちらを拝借しました
https://qiita.com/oishihiroaki/items/6eb9732efb44d4986428
そもそもAWSやSQL自体が初めてで何も分かっていません。
根本的なところが違うのだろうとは思いますが、まず何からすれば良いのか分かりません。
回答に必要な情報がございましたら教えて頂ければ幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/16 10:56