unity PC 、安卓修改数据库文件

详情参考https://www.cnblogs.com/xiaoyulong/p/8686886.html

MySqlAccess脚本是别人封装好的

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

public class MySqlAccess
{
    public static MySqlConnection mySqlConnection;//连接类对象

    private static string host;     //IP地址。如果只是在本地的话,写localhost就可以。
    private static string id;       //用户名。
    private static string pwd;      //密码。
    private static string dataBase; //数据库名称。

    /// 
    /// 构造方法
    /// 
    /// IP地址
    /// 用户名
    /// 密码
    /// 数据库名称
    public MySqlAccess(string _host, string _id, string _pwd, string _dataBase)
    {
        host = _host;
        id = _id;
        pwd = _pwd;
        dataBase = _dataBase;
        OpenSql();
    }

    ///   
    /// 打开数据库  
    ///   
    public static void OpenSql()
    {
        try
        {
            //string.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项。  
            string mySqlString = string.Format("Database={0};Data Source={1};User Id={2};Password={3};CharSet=utf8", dataBase, host, id, pwd, "3306");

            mySqlConnection = new MySqlConnection(mySqlString);
            mySqlConnection.Open();
        }
        catch (Exception e)
        {
            throw new Exception("服务器连接失败,请重新检查是否打开MySql服务。" + e.Message.ToString());
        }
    }

    ///   
    /// 创建表  
    ///   
    /// 表名  
    /// 属性列  
    /// 属性类型  
    ///   
    public DataSet CreateTable(string name, string[] colName, string[] colType)
    {
        if (colName.Length != colType.Length)
        {
            throw new Exception("输入不正确:" + "columns.Length != colType.Length");
        }
        string query = "CREATE TABLE  " + name + "(" + colName[0] + " " + colType[0];
        for (int i = 1; i < colName.Length; i++)
        {
            query += "," + colName[i] + " " + colType[i];
        }
        query += ")";
        return QuerySet(query);
    }

    ///   
    /// 创建具有id自增的表  
    ///   
    /// 表名  
    /// 属性列  
    /// 属性列类型  
    ///   
    public DataSet CreateTableAutoID(string name, string[] col, string[] colType)
    {
        if (col.Length != colType.Length)
        {
            throw new Exception("columns.Length != colType.Length");
        }
        string query = "CREATE TABLE  " + name + " (" + col[0] + " " + colType[0] + " NOT NULL AUTO_INCREMENT";
        for (int i = 1; i < col.Length; ++i)
        {
            query += ", " + col[i] + " " + colType[i];
        }
        query += ", PRIMARY KEY (" + col[0] + ")" + ")";
        return QuerySet(query);
    }

    ///   
    /// 插入一条数据,包括所有,不适用自动累加ID。  
    ///   
    /// 表名  
    /// 插入值  
    ///   
    public DataSet InsertInto(string tableName, string[] values)
    {
        string query = "INSERT INTO " + tableName + " VALUES (" + "'" + values[0] + "'";
        for (int i = 1; i < values.Length; ++i)
        {
            query += ", " + "'" + values[i] + "'";
        }
        query += ")";
        return QuerySet(query);
    }
    ///   
    /// 插入部分ID  
    ///   
    /// 表名  
    /// 属性列  
    /// 属性值  
    ///   
    public DataSet InsertInto(string tableName, string[] col, string[] values)
    {
        if (col.Length != values.Length)
        {
            throw new Exception("columns.Length != colType.Length");
        }
        string query = "INSERT INTO " + tableName + " (" + col[0];
        for (int i = 1; i < col.Length; ++i)
        {
            query += ", " + col[i];
        }
        query += ") VALUES (" + "'" + values[0] + "'";
        for (int i = 1; i < values.Length; ++i)
        {
            query += ", " + "'" + values[i] + "'";
        }
        query += ")";
        return QuerySet(query);
    }

    ///   
    /// 查询表数据 
    ///   
    /// 表名  
    /// 需要查询的列  
    /// 查询的条件列  
    /// 条件操作符  
    /// 条件的值  
    ///   
    public DataSet Select(string tableName, string[] items, string[] whereColName, string[] operation, string[] value)
    {
        if (whereColName.Length != operation.Length || operation.Length != value.Length)
        {
            throw new Exception("输入不正确:" + "col.Length != operation.Length != values.Length");
        }
        string query = "SELECT " + items[0];
        for (int i = 1; i < items.Length; i++)
        {
            query += "," + items[i];
        }
        query += "  FROM  " + tableName + "  WHERE " + " " + whereColName[0] + operation[0] + " '" + value[0] + "'";
        for (int i = 1; i < whereColName.Length; i++)
        {
            query += " AND " + whereColName[i] + operation[i] + "' " + value[i] + "'";
        }
        return QuerySet(query);
    }

    ///   
    /// 更新表数据 
    ///   
    /// 表名  
    /// 更新列  
    /// 更新的值  
    /// 条件:列  
    /// 条件:值  
    ///   
    public DataSet UpdateInto(string tableName, string[] cols, string[] colsvalues, string selectkey, string selectvalue)
    {
        string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0];
        for (int i = 1; i < colsvalues.Length; ++i)
        {
            query += ", " + cols[i] + " =" + colsvalues[i];
        }
        query += " WHERE " + selectkey + " = " + selectvalue + " ";
        return QuerySet(query);
    }

    ///   
    /// 删除表数据  
    ///   
    /// 表名  
    /// 条件:删除列  
    /// 删除该列属性值所在得行  
    ///   
    public DataSet Delete(string tableName, string[] cols, string[] colsvalues)
    {
        string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0];
        for (int i = 1; i < colsvalues.Length; ++i)
        {
            query += " or " + cols[i] + " = " + colsvalues[i];
        }
        return QuerySet(query);
    }

    /// 
    /// 释放
    /// 
    public void Close()
    {
        if (mySqlConnection != null)
        {
            mySqlConnection.Close();
            mySqlConnection.Dispose();
            mySqlConnection = null;
        }
    }

    ///     
    /// 执行Sql语句  
    ///   
    /// sql语句  
    ///   
    public static DataSet QuerySet(string sqlString)
    {
        if (mySqlConnection.State == ConnectionState.Open)
        {
            DataSet ds = new DataSet();
            try
            {
                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sqlString, mySqlConnection);
                mySqlDataAdapter.Fill(ds);
            }
            catch (Exception e)
            {
                throw new Exception("SQL:" + sqlString + "/n" + e.Message.ToString());
            }
            finally
            {
            }
            return ds;
        }
        return null;
    }
}

Sql_test是测试用的

using UnityEngine;
using System.Data;
using UnityEngine.UI;

public class Sql_test : MonoBehaviour
{
    [Header("添加")]
    public InputField date_city_add;
    public InputField date_school_add;
    public InputField date_totels_add;
    public InputField ndate_students_add;
    //[Header("删除")]
    //public InputField date_city_dele;
    //public InputField date_school_dele;
    //public InputField date_totels_dele;
    //public InputField ndate_students_dele;
    //[Header("修改")]
    //public InputField date_id_change;
    //public InputField date_city_change;
    //public InputField date_school_change;
    //public InputField date_totels_change;
    //public InputField ndate_students_change;
    //[Header("查看")]
    //public InputField date_city_look;
    //public InputField date_school_look;
    //public InputField date_totels_look;
    //public InputField ndate_students_look;

    public MySqlAccess mySql;
    //添加
    public Button add_button;
    删除
    //public Button delet_button;
    提交修改
    //public Button submit_button;
    查看
    //public Button look_button;
    private void Start()
    {
         mySql = new MySqlAccess("IP", "用户名", "密码", "数据库名称");


      
        //change_button.onClick.AddListener(Update_demo(string id, string city, string school, string hotels, string students));


       mySql.CreateTableAutoID("test", new string[] { "id", "city", "school", "hotels", "students" }, new string[] { "int", "text", "text", "int" , "int" });

        add_button.onClick.AddListener(delegate () { Add_demo("北京", "北大", "45", "90"); });

        //mySql.InsertInto("tableTest", new string[] { "name", "age" }, new string[] { "wanger", "28" });
        //mySql.InsertInto("tableTest", new string[] { "name", "age" }, new string[] { "liger", "2558" });
        // mySql.UpdateInto("tableTest", new string[] { "name", "age" }, new string[] { "name", "10" },"id","1");

        //mySql.Delete("tableTest", new string[] { "name", "age" }, new string[] { "'liger'", "10" });

        //mySql.InsertInto("Test", new string[] { "name", "age" }, new string[] { "李四", "20" });
        //for (int i = 1; i < 3; i++)
        //{
        //    DataSet ds = mySql.Select("tableTest", new string[] { "name", "age" }, new string[] { "id" }, new string[] { "=" }, new string[] { i.ToString() });
        //    if (ds != null)
        //    {
        //        DataTable table = ds.Tables[0];
        //        foreach (DataRow row in table.Rows)
        //        {
        //            foreach (DataColumn column in table.Columns)
        //            {
        //                Debug.Log(row[column]);
        //            }
        //        }
        //    }
        //}
        //mySql.Close();
    }
    //数据更新
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {

            string str =string.Format("{0}", date_city_add.GetComponent().text);
            string str1 =string.Format("{0}", date_school_add.GetComponent().text);
            Add_demo(str, str1, "45", "90");
        }          
    }
    //更新 
    public void Update_demo(string id,string city,string school,string hotels,string students)
    {
        mySql.UpdateInto("tableTest", new string[] { "id", "city" , "school", "hotels", "students" }, new string[] { city, school, hotels, students }, "id", id);
        mySql.Close();
    }
    //删除 需要''
    public void Deleted_demo(string city, string school, string hotels, string students)
    {
        mySql.Delete("tableTest", new string[] { "city", "school", "hotels", "students" }, new string[] { city, school, hotels, students });
        mySql.Close();
    }
    //添加 不需要''
    public void Add_demo(string city, string school, string hotels, string students)
    {
        mySql.InsertInto("test", new string[] { "city", "school", "hotels", "students" }, new string[] { city, school , hotels , students });
        mySql.Close();
    }
    //查询
    public void Select_demo(int number, string city, string school, string hotels, string students)
    {
        for (int i = 1; i < number; i++)
        {
            DataSet ds = mySql.Select("tableTest", new string[] { "city", "school", "hotels", "students" }, new string[] { "id" }, new string[] { "=" }, new string[] { i.ToString() });
            if (ds != null)
            {
                DataTable table = ds.Tables[0];
                foreach (DataRow row in table.Rows)
                {
                    foreach (DataColumn column in table.Columns)
                    {
                        Debug.Log(row[column]);
                    }
                }
            }
        }
     
        mySql.Close();
    }  

}

 

你可能感兴趣的:(unity,脚本,c#,数据库)