unity读取Sqlite数据库

using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.Data; public enum DataBaseMessage //用来存放数据库的异常信息
{
Normal = , //正常
AddDataExist, //添加的数据已经存在
PrimaryKeyNoExist, //主键不存在
}
//用来对数据库的操作
public class DataBaseManager //对数据库的操作,封装成单例模式
{
public static DataBaseManager GetInstanic
{
get
{
if (Instatinc == null)
Instatinc = new DataBaseManager();
return Instatinc;
}
}
private DataBaseManager() //构造函数
{
SQLConn = new SqliteConnection(SQL_Path); //创建数据连接
SQLComm = SQLConn.CreateCommand(); //创建对应的数据库指令
} public void Open_DB() //打开数据库
{
if (SQLConn.State == ConnectionState.Closed) //判断当前数据库连接的状态是否在关闭状态
{
SQLConn.Open(); //如果当前的数据库连接状态为关闭状态,就开启当前状态
}
} public void Close_DB() //关闭数据库
{
SQLConn.Close(); //关闭数据库的时候,不需要判断当前的数据库状态,直接关闭即可 } public DataBaseMessage Add_Data(string Table, string ColumnName, string data) //往数据库里面添加数据
{
string comm = "Insert Into " + Table + "( "+ ColumnName +" ) Values ( '" + data +"')";
SQLComm.CommandText = comm; //执行的数据库指令
try
{
SQLComm.ExecuteNonQuery(); //执行SQL指令
}
catch(System.Exception e) //用来抛出异常信息
{
string str = "column " + ColumnName +" is not unique"; //数据库异常信息: 该列的值不能重复,但是尝试插入重复的值
if (str == ExceptionMessage(e.Message, "\n"))
{
return DataBaseMessage.AddDataExist; //如果已经存在了,就返回存在的信息
}
}
return DataBaseMessage.Normal;
} public DataBaseMessage Add_Data(string Table, string ColumnName, string PrimaryKey, string PrimaryKeyValue, string data)
{
//添加到不是主键的里面
//Update PlayCount set Pass = 25 Where User = '1'
string comm = "Update "+ Table +" set " +ColumnName+" = '" + data +"' Where " + PrimaryKey+ "= '"+ PrimaryKeyValue +"'";
SQLComm.CommandText = comm; //执行的数据库指令
if(SQLComm.ExecuteNonQuery() == ) //执行SQL指令
{
return DataBaseMessage.PrimaryKeyNoExist; //如果执行指令的影响行数是零,说明没有此行,既没有对应的PrimaryKeyValue
}
return DataBaseMessage.Normal;
} public bool Select_Data(string Table, string ColumnName, string PrimaryKey, string PrimaryKeyValue, string data)
{
//判断某一特定的行,对应的某一列上面的值是否存在
string comm = "Select " +ColumnName +" From " + Table +" Where " + PrimaryKey +" = '" + PrimaryKeyValue +"'"; //只有一行
SQLComm.Cancel(); //先取消命令,在不取消命令的情况下,不能重新设置命令
SQLComm.CommandText = comm;
Read = SQLComm.ExecuteReader(); //执行命令
if(Read.GetValue().ToString() == data) //判断是否相等,如果相等就返回真
{
Read.Close();
return true;
}
Read.Close();
return false; //不存在 } public bool Select_Data(string Table, string ColumnName, string data) //判断某一行是否存在该数据
{
string comm = "Select " + ColumnName +" From " + Table; //查询某一列的值
SQLComm.Cancel();
SQLComm.CommandText = comm;
Read = SQLComm.ExecuteReader();
while (Read.Read())
{
if (Read.GetValue().ToString() == data)
{
Read.Close();
return true; //如果存在就返回真
}
}
Read.Close();
return false;
} public string Get_Data(string Table, string ColumnName, string PrimaryKey, string PrimaryKeyValue)
{ //取出数据库里面某一个表,某一列,某一行的值
string comm = "Select " + ColumnName + " From " + Table + " Where " + PrimaryKey + " = '" + PrimaryKeyValue + "'"; //只有一行
SQLComm.Cancel(); //先取消命令,在不取消命令的情况下,不能重新设置命令
SQLComm.CommandText = comm;
Read = SQLComm.ExecuteReader(); //执行命令
string data = Read.GetValue().ToString();
Read.Close();
return data;
} private string ExceptionMessage(string ExcMessage, params string[] s) //分割异常信息
{
string[] str = ExcMessage.Split(s, System.StringSplitOptions.RemoveEmptyEntries);
return str[str.Length - ].ToString();
}
private static DataBaseManager Instatinc;
private SqliteConnection SQLConn; //创建数据库连接对象
private SqliteCommand SQLComm; //创建数据库指令对象
private SqliteDataReader Read; //创建数据库连接读取对象
private string SQL_Path = "Data Source = " +Application.dataPath + "/_MyProject/Plugins/Demo_Defense_DB"; //数据库路径 }
上一篇:SQL你必须知道的-查询聚合分组排序


下一篇:Jquery操作下拉框(DropDownList)实现取值赋值