访问数据库 委托事件返回数据

技术都是慢慢积累的,为了程序的简洁和模块化,我开始喜欢写单例类,这里写了一个访问数据库的单例类,将访问后得到的数据,作为一个委托事件的参数传出去,然后再在其他地方进行处理。因为考虑到会使用中文,这里只写了POST访问。数据库,用的是UPUPW集成工具。


访问数据库,单例类:

using UnityEngine;
using System.Collections;

public class DataBaseModel 
{
    private static DataBaseModel dataBase;
    /// <summary>
    /// 委托函数
    /// </summary>
    /// <param name="tag">标签</param>
    /// <param name="resultData">获取到数据</param>
    public delegate void ReturnResultData(string tag,string resultData);
    /// <summary>
    /// 事件
    /// </summary>
    public event ReturnResultData getData;
    /// <summary>
    /// php的路径
    /// </summary>
    public string phpPath;

    ///<summary>
    /// 初始化
    /// </summary>
    /// <returns></returns>
    public static DataBaseModel getInstance()
    {
        if (dataBase == null)
        {
            dataBase = new DataBaseModel();
            dataBase.phpPath = "lgxy.hrbu.edu.cn:8080/";
        }
        return dataBase;
    }

    /// <summary>
    /// 将数据库中的数据读出
    /// </summary>
    /// <param name="strValue">变量</param>
    /// <param name="strData">参数</param>
    /// <param name="phpPathName">php文件名</param>
    /// <returns></returns>
    public IEnumerator ReadMySQLData(string[] strValue, string[] strData, string phpName)
    {
        WWWForm wf = new WWWForm();
        for (int i = 0; i < strValue.Length; i++)
        {
            wf.AddField(strValue[i], strData[i]);
          //  path+=("&"+strValue[i]+"="+strData[i]);
        }
        WWW w = new WWW("http://" + phpPath + phpName+"?", wf);
        yield return w;
        if (w.error == null)
            getData(phpName, w.text.Trim());
    }

    /// <summary>
    /// 将数据写入数据库
    /// </summary>
    /// <param name="strValue">变量</param>
    /// <param name="strData">参数</param>
    /// <param name="phpPathName">php文件名,例如:login.php</param>
    /// <returns></returns>
    public IEnumerator WriteMySQLData(string[] strValue, string[] strData, string phpName)
    {
        WWWForm wf = new WWWForm();
        for (int i = 0; i < strValue.Length; i++)
        {
            wf.AddField(strValue[i], strData[i]);
        }
       
        WWW w = new WWW("http://"+phpPath + phpName+"?", wf);
        yield return w;
    }
}


测试类:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    void OnEnable()
    {
        //为DataBaseModel的getData事件  添加函数
        DataBaseModel.getInstance().getData += Test_getData;
    }

    void OnDisable()
    {
        DataBaseModel.getInstance().getData -= Test_getData;
    }


	// Use this for initialization
	void Start () {
        //在数据库中写入数据
       string[] strValue = new string[] {"ID","Name"};
       string[] strData = new string[] { "20150101", "赵壹" };
       StartCoroutine(DataBaseModel.getInstance().WriteMySQLData(strValue, strData,"login.php"));

       //在数据库中读取数据  
       //在调用ReadMySQLData函数的时候  返回数据 就会执行Test_getData函数
       string[] strNewValue = new string[] { "ID" };
       string[] strNewData = new string[] { "20150101" };
       StartCoroutine(DataBaseModel.getInstance().ReadMySQLData(strNewValue, strNewData, "readInfor.php"));
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    /// <summary>
    /// 处理获取的数据
    /// </summary>
    /// <param name="tag">标签,访问的php名称</param>
    /// <param name="resultData">返回的数据</param>
    void Test_getData(string tag, string resultData)
    {
        switch (tag)
        {
            case "readInfor.php":
                Debug.Log("访问readInfor.php获取到的数据");
                break;
            default:
                break;
        }
    }
}


原文地址:https://www.cnblogs.com/liang123/p/6325909.html