Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
43
Help us understand the problem. What is going on with this article?
@oishihiroaki

UnityでMySQLを使用する

More than 5 years have passed since last update.

UnityでMySQLを使用する

実行環境 :
MacOS 10.7.5, Windows8.1
Unity4.3.1

MySQL Connectorのダウンロード

http://dev.mysql.com/downloads/connector/net/
こちらよりMono用のMySQL Connectorをダウンロードします.

Select Platform: で .NET & Mono を選択

ConnectorをUnityにインポート

ダウンロードして得られたファイルの中で、
mysql-connector-net-x.x.x-noinstall > v2.0 > mysql.data.dll
をUnityAsset内の Plugins フォルダに入れる.

Api Compatibility Level を変更

Player Settings > Optimization > Api Compatibility Level を
.NET 2.0 に変更

コード例


using UnityEngine;
using System;
using System.Collections;

using MySql.Data;
using MySql.Data.MySqlClient;

public class MySQLTest : MonoBehaviour {

    string SERVER    = "localhost";
    string DATABASE  = "database";
    string USERID    = "root";
    string PORT      = "3306";
    string PASSWORD  = "pass";
        string TABLENAME = "hoge";

    void Start () {

        StartCoroutine ( SelectData () );

    }

    IEnumerator SelectData () {

        MySqlConnection con = null;

        string conCmd = 
                "server="+SERVER+";" +
                "database="+DATABASE+";" +
                "userid="+USERID+";" +
                "port="+PORT+";" +
                "password="+PASSWORD;

        try {

            con = new MySqlConnection( conCmd );
            con.Open ();

        } catch (MySqlException ex){
            Debug.Log ( ex.ToString() );
        }

        string selCmd = "SELECT * FROM TABLENAME LIMIT 0, 1200;";

        MySqlCommand cmd = new MySqlCommand( selCmd, con );

        IAsyncResult iAsync = cmd.BeginExecuteReader ();

        while ( !iAsync.IsCompleted ){
            yield return 0;
        }

        MySqlDataReader rdr = cmd.EndExecuteReader ( iAsync );

        while ( rdr.Read() ) {
            if( !rdr.IsDBNull ( rdr.GetOrdinal ("ID") ) ){
                //Debug.Log ( "ID : " + rdr.GetString ("ID") );
            }
        }

        rdr.Close ();
        rdr.Dispose ();
        con.Close ();
        con.Dispose ();
    }
}


※非同期で動きません (> <)

43
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
oishihiroaki
Interactive Design & Art / Engineer Senior Lecturer, Department of Design Informatics, Musashino Art University

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
43
Help us understand the problem. What is going on with this article?