【Unity】封装SQLite管理类

发表于2017-08-04
评论0 406浏览

想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学院游戏程序行业精英群

711501594
本篇文章给大家介绍下封装SQLite管理类的使用。

代码如下:
using UnityEngine;  
using System.Collections;  
using Mono.Data.Sqlite;  
using System.Data.SqlClient;  
public class SQLiteManager  
{  
    private SqliteConnection con;  
    private SqliteCommand command;  
    // 构造函数重载  
    public SQLiteManager (string dataBasePath)  
    {  
        // 链接数据库  
        con = new SqliteConnection (dataBasePath);  
        // 打开数据库  
        con.Open ();  
        // 实例化命令  
        command = con.CreateCommand ();  
    }  
    // 增--方式1  
    // insert into 表名  values(值1,值2...)  
    public SqliteDataReader InsertData (string tableName, object[] values)  
    {  
        command.CommandText = "insert into "   tableName   " values ("   values [0];  
        for (int i = 1; i < values.Length; i ) {  
            command.CommandText  = ","   values [i];  
        }  
        command.CommandText  = ")";  
        return command.ExecuteReader ();  
    }  
    // 增--方式2  
    public SqliteDataReader InsertData (string tableName, string[] cols, object[] values)  
    {  
        if (values.Length != cols.Length) {  
            Debug.Log ("参数个数与列数不匹配,无法进行数据的写入!");  
            return null;  
        }  
        // insert into 表名(列0,列1) values(值1,值2)  
        // into后面有空格!!!  
        command.CommandText = "insert into "   tableName   "("   cols [0];  
        // 把每个列的名字,都加入到  表名(列0,列1  
        for (int i = 1; i < cols.Length; i ) {  
            command.CommandText  = ","   cols [i];  
        }  
        command.CommandText  = ")";  
        command.CommandText  = " values("   values [0];  
        for (int i = 1; i < values.Length; i ) {  
            command.CommandText  = ","   values [i];  
        }  
        command.CommandText  = ")";  
        return command.ExecuteReader ();  
    }  
    //-----------------------------------------  
    // 删  
    public SqliteDataReader DeleteData (string tableName)  
    {  
        // 清空当前表中所有的内容  
        command.CommandText = "delete from "   tableName;  
        return command.ExecuteReader ();  
    }  
    // 删除满足条件的指定数据  
    public  SqliteDataReader DeleteData (string tableName, string condition)  
    {  
        command.CommandText = "delete from "   tableName   " where "   condition;  
        return command.ExecuteReader ();  
    }  
    //-----------------------------------------  
    // 改  
    public SqliteDataReader UpdateData (string tableName, string[] cols, object[] values, string condition)  
    {  
        if (values.Length != cols.Length) {  
            Debug.Log ("输入的值的个数有误!");  
            return null;  
        }  
        command.CommandText = "update "   tableName   " set "   cols [0]   "="   values [0];  
        for (int i = 1; i < cols.Length; i ) {  
            command.CommandText  = ","   cols [i]   "="   values [i];  
        }  
        command.CommandText  = " where "   condition;  
        return command.ExecuteReader ();  
    }  
    //-----------------------------------------  
    // 查  
    // string condition = null:设置参数的默认值为null  
    // 调用该方法的时候,可以选择传递3个参数,或者2个参数,如果传递2个参数,第三个参数的值将使用默认值  
    public SqliteDataReader SelectData (string tableName, string[] cols, string condition = null)  
    {  
        command.CommandText = "select "   cols [0];  
        for (int i = 1; i < cols.Length; i ) {  
            command.CommandText  = ","   cols [i];  
        }  
        command.CommandText  = " from "   tableName;  
        if (condition != null) {  
            command.CommandText  = " where "   condition;  
        }  
        return command.ExecuteReader ();  
    }  
    //-----------------------------------------  
    // 关闭数据库  
    public void CloseDataBase ()  
    {  
        con.Close ();  
        command.Cancel ();  
    }  
}  

原文链接

著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签:

游戏学院公众号二维码
腾讯游戏学院
微信公众号

提供更专业的游戏知识学习平台