using Mono.Data.Sqlite;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TaskDataBase {
//任务系统数据库的单例
static TaskDataBase _instance;
public static TaskDataBase Instance()
{
if (_instance == null)
{
return _instance = new TaskDataBase();
}
return _instance;
}
private TaskDataBase()
{
//单例 禁止外界访问 直接私有化处理
}
//数据库链接对象头SqliteConnection
SqliteConnection conn=null;
public void ConnectionDB(string path)
{
path = "data source=" + path;
if (conn == null)
{
conn = new SqliteConnection(path );
}else
{
Debug.Log("数据库已经连接了");
}
}
public void OpenDB()
{
if (conn == null)
{
Debug.Log("没有链接到数据库");
}
try
{
conn.Open();
}catch(System.Exception exp)
{
Debug.Log(exp);
}
}
public void CloseDB()
{
if (conn == null)
{
Debug.Log("没有链接到数据库");
}
try
{
conn.Close();
}catch(System.Exception exp)
{
Debug.Log(exp);
}
}
//提供非查询操作
public void ExecuteNonQuery(string sql)
{
OpenDB();
SqliteCommand sqlcmd= conn.CreateCommand();
sqlcmd.CommandText = sql;
sqlcmd.ExecuteNonQuery();
sqlcmd.Dispose();
sqlcmd = null;
CloseDB();
}
public List<ArrayList>ExecuteQuery(string sql)
{
OpenDB();
SqliteCommand sqlcmd = conn.CreateCommand();
sqlcmd.CommandText = sql;
List<ArrayList> list = new List<ArrayList>();
try {
SqliteDataReader reader = sqlcmd.ExecuteReader();
while (reader.Read())
{
ArrayList arr = new ArrayList();
for(int i = 0; i < reader.FieldCount; i++)
{
arr.Add(reader.GetValue(i));
}
list.Add(arr);
}
}
catch (System.Exception exp)
{
Debug.Log(exp);
}
CloseDB();
return list;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TaskDataBase {
//任务系统数据库的单例
static TaskDataBase _instance;
public static TaskDataBase Instance()
{
if (_instance == null)
{
return _instance = new TaskDataBase();
}
return _instance;
}
private TaskDataBase()
{
//单例 禁止外界访问 直接私有化处理
}
//数据库链接对象头SqliteConnection
SqliteConnection conn=null;
public void ConnectionDB(string path)
{
path = "data source=" + path;
if (conn == null)
{
conn = new SqliteConnection(path );
}else
{
Debug.Log("数据库已经连接了");
}
}
public void OpenDB()
{
if (conn == null)
{
Debug.Log("没有链接到数据库");
}
try
{
conn.Open();
}catch(System.Exception exp)
{
Debug.Log(exp);
}
}
public void CloseDB()
{
if (conn == null)
{
Debug.Log("没有链接到数据库");
}
try
{
conn.Close();
}catch(System.Exception exp)
{
Debug.Log(exp);
}
}
//提供非查询操作
public void ExecuteNonQuery(string sql)
{
OpenDB();
SqliteCommand sqlcmd= conn.CreateCommand();
sqlcmd.CommandText = sql;
sqlcmd.ExecuteNonQuery();
sqlcmd.Dispose();
sqlcmd = null;
CloseDB();
}
public List<ArrayList>ExecuteQuery(string sql)
{
OpenDB();
SqliteCommand sqlcmd = conn.CreateCommand();
sqlcmd.CommandText = sql;
List<ArrayList> list = new List<ArrayList>();
try {
SqliteDataReader reader = sqlcmd.ExecuteReader();
while (reader.Read())
{
ArrayList arr = new ArrayList();
for(int i = 0; i < reader.FieldCount; i++)
{
arr.Add(reader.GetValue(i));
}
list.Add(arr);
}
}
catch (System.Exception exp)
{
Debug.Log(exp);
}
CloseDB();
return list;
}
}