美文网首页
数据库框架

数据库框架

作者: 醉酒青牛_fa4e | 来源:发表于2019-04-14 13:44 被阅读0次

    数据库框架
    数据库共6个操作,连接数据库,建表,增,删,改,查,
    此为两个工具类
    一、字段属性准备
    1、 public static readonly SQLiteManager Instance = new SQLiteManager();
    2、构造方法用protectedprotected SQLiteManager() {}
    3、定义一个字符串,用来存储数据库路径private string databasePath;
    4、定义三个类的private变量
    private SqliteConnection con;
    private SqliteCommand command;
    private SqliteDataReader reader;
    二、封装方法
    1、连接到指定数据库,并打开
    /// <summary>
    /// 连接到指定数据库,并打开它.
    /// </summary>
    /// <param name="databasename">数据库名称.</param>
    public void OpenDatabase(string databasename)
    {
    //动态添加后缀
    if (!databasename.EndsWith (".sqlite")) {
    databasename += ".sqlite";
    }
    //填写路径
    databasePath = "Data Source = " + Application.streamingAssetsPath
    + "/" + databasename;
    Try{
    //实例化连接对象,连接数据库
    con = new SqliteConnection (databasePath);
    //打开连接
    con.Open ();
    //创建指令对象
    command = con.CreateCommand ();

    }catch(SqliteException e)
    {
    Debug.logWarning(“连接数据库失败:”+e.ToString());
    }
    }

    1.1、定义一个public,返回值void方法OpenDataBase,参数为string dataBaseName(数据库名)
    1.2、判断参数(数据库名)是否以.sqlite为结尾,如果不是,动态添加//动态添加后缀
    if (!databasename.Contains (".sqlite")) {
    databasename += ".sqlite";
    }
    1.3、填写路径
    //填写路径
    databasePath = "Data Source = " + Application.streamingAssetsPath+ "/" + databasename;
    "Data Source = "为固定格式,动态数据放在streamingAssets文件夹

    1.4、实例化连接对象,把数据库路径传给con
    //实例化连接对象
    con = new SqliteConnection (databasePath);
    1.5、打开数据库
    Con.Open();
    1.6、创建指令对象,用command接收
    command=con.CreateCommand();

    2、封装关闭数库据方法
    定义一个public,void返回值,无参的CloseDataBase方法
    判断如果数据库已经连接,可以关闭
    public void Close()
    {
    if (con != null) {
    con.Close ();
    }
    }
    在析构方法中关闭数据库,也就是说在脚本生命周期结束的最后一瞬间关闭数据库
    3、封装增删改操作方法
    定义一个public,void返回值,参数为string(方法体内要执行的SQL语句)的方法
    public int ExecuteNonQuery(string sqlQuery)
    {
    //设置SQL语句
    command.CommandText = sqlQuery;
    try {
    //执行SQL语句
    return command.ExecuteNonQuery();
    } catch (System.Exception ex) {
    //抛出异常
    Debug.Log (“执行SQL语句异常:”+ex.ToString());
    return 0;
    }
    }

    方法体内先设置sql语句->执行SQL语句,执行SQL语句时可能出错,通过try{}catch(){}捕获一下异常
    4、封装查询所有数据的方法
    定义一个public,返回值list<Dictionary<string,object>>,此返回值为数据库中要存储的数据类型,参数为string(SQL语句)的方法
    在方法内先声明一个返回值类型的集合,用来存储执行SQL语句查询到的结果,用while()循环遍历表中的数据,查询到的每一条数据以键值对的形式存储起来
    用SqliteDataReader.FieldCount属性拿到当前这一行数据的列数,用for循环遍历读到这一行的所有列,并将所有数据添加到键值对集合中。
    而后再把这个键值对集合添加到外层集合(返回值的外层集合)
    在while循环外关闭SqliteDataReader对象
    由于以上操作可能出现异常,所以用try{}catch(){}捕获一下异常。
    把添加好的list<Dictionary<string,object>>集合返回即可
    public List<Dictionary<string, object>> ExecuteQuery(string sql)
    {
    // 存储所有查询结果的集合, 查询到的每一条数据以键值对的形式存在
    List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();

        command.CommandText = sql;
    
        try
        {
            reader = command.ExecuteReader();
    
            while (reader.Read())
            {
                // 说明读到了一行数据, 将这一行数据做成键值对
                Dictionary<string, object> item = new Dictionary<string, object>();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    // 将列名和列的值存到Item中
                    item.Add(reader.GetName(i), reader.GetValue(i));
                }
                // 添加一行数据到集合中
                result.Add(item);
            }
    
            reader.Close();
        }
        catch (SqliteException e)
        {
            Debug.LogWarning("数据查询异常: " + e.ToString());
        }
    
        return result;
    }
    

    三、第一个工具类封装->打开、关闭数据库方法,连接到指定数据库方法,执行一个查询语句方法,执行查询所有数据方法。
    打开数据库在连接到指定数据库方法中的创建操作指令对象后调用,关闭数据库在析构方法中调用.
    连接到指定数据库、执行一个非查询语句,查询所有数据等3个方法都以SQL语句为参数,在第二个工具类里调用,并传一个SQL语句。
    四、第二个工具类
    1、设置为单例脚本
    public static readonly new SQLiteTool Instance = new SQLiteTool();
    2、定义一个加载数据库方法,方法体内调用父类加载数据库方法,并把数据库名字传参,在私有化的构造方法内调用此加载数据库方法,意思是用到此工具类的时候先创建并链接数据库。

    3、建表。
    定义一个建表方法,用常量字段作为表名和表字段名
    private void CreateTable()
    {
    ExecuteNonQuery(string.Format("create table if not exists {0}({1} integer primary key, {2} integer);", Utlity.TableName, Utlity.KEY_LevelID, Utlity.KEY_LevelStar));
    }
    4、定义一个常量类,用来做数据库表和字段
    static class Utlity
    {
    public static readonly string TableName = "Level";
    public static readonly string KEY_LevelID = "LevelID";
    public static readonly string KEY_LevelStar = "LevelStar";
    }

    5、定义一个信息类,用来给表内的字段赋值,此时假设表内只有两个字段
    public class Info
    {
    public int id;
    public int star;

    public Info(int id, int star)
    {
        this.id = id;
        this.star = star;
    }
    

    }
    6、封装AddInfo方法,用来增加一条表中数据
    public void AddInfo(Info info)
    {
    ExecuteNonQuery(string.Format("insert into {0} values ({1}, {2})", Utlity.TableName, info.id, info.star));
    }

    6、封装一个AddInfo的重载方法,方便不同情况下增加一条表中的数据。在此方法中调用上面方法,并传参,实现通过表中一条数据需要的值来添加一条信息,也可以再封装几个重载方法,用来添加没有所有字段值的一条数据。
    public void AddInfo(int id, int star)
    {
    AddInfo(new Info(id, star));
    }
    7、封装一个修改数据的方法,返回值为void,参数为表中一条数据对应的值(根据情况自行设置),方法体内调用第一个工具类中的执行一条非查询语句的方法
    public void UpdateInfo(int id, int newStar)
    {
    ExecuteNonQuery(string.Format("update {0} set {1} = {2} where {3} = {4};", Utlity.TableName, Utlity.KEY_LevelStar, newStar, Utlity.KEY_LevelID, id));
    }

    8、封装一个获取指定字段的值的方法
    返回值为指定字段值的数据类型,参数为查询条件。
    方法体内声明一个集合->接收执行一个查询语句的结果(List<Dictionary<string, object>>,此为第一个工具类定义的执行一个非查询语句的返回值类型),而后做一个判断,如果结果(结果为键值对)的Count==0,也就是说没有查询到想要的数据,我们返回-1,表示没有查询到。如果查询到了,我们把这个值拿出来(查询数据的条件理应保持唯一性)。
    public int GetStar(int id)
    {
    List<Dictionary<string, object>> result = ExecuteQuery(string.Format("select {0} from {1} where {2} = {3}", Utlity.KEY_LevelStar, Utlity.TableName, Utlity.KEY_LevelID, id));
    if (result.Count == 0) return -1;

        Dictionary<string, object> item = result[0];
        return (int)(long)item[Utlity.KEY_LevelStar];
    }
    

    9、封装一个方法,用来查询所有信息。返回值为list<信息类>,无参。
    声明一个泛型集合1,用来接收查询到的信息,并作返回使用
    声明一个泛型集合2,用来接收执行一个非查询语句的结果
    遍历查询到的数据所存储的集合,拿到键值对的所有数据,并把这些数据添加到泛型集合1中,作为返回值。
    public List<StarInfo> QueryAll()
    {
    // 存储所有查询结果的集合
    List<StarInfo> result = new List<StarInfo>();
    // 从数据库中查询到的信息
    List<Dictionary<string, object>> queryResult = ExecuteQuery(string.Format("select * from {0}", Utlity.TableName));

        foreach (Dictionary<string, object> item in queryResult)
        {
            int id = (int)(long)item[Utlity.KEY_LevelID];
            int star = (int)(long)item[Utlity.KEY_LevelStar];
    
            // 将id和star做成一个StarInfo对象, 存到集合中
            result.Add(new StarInfo(id, star));
        }
    
        return result;
    }

    相关文章

      网友评论

          本文标题:数据库框架

          本文链接:https://www.haomeiwen.com/subject/hewhiqtx.html