using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;
using System.IO;
public class DataBasic {
//单例
private static DataBasic instance;
private DataBasic() { }
public static DataBasic Instance
{
get
{
if (instance == null)
{
instance = new DataBasic();
}
return instance;
}
}
private SqliteConnection SqlCon;
private string ConnectPath;
bool ConnectDataBase()
{
ConnectPath = "data source=" + Application.streamingAssetsPath + "/individualeventdatabase.sqlite";
SqlCon = new SqliteConnection(ConnectPath);
//判断是否存在数据库
if (SqlCon != null)
{
//如果存在,打来数据库
SqlCon.Open();
return true;
}
else
{
Debug.Log("数据库打开失败");
return false;
}
}
//判断是否连接数据库(此方法只在安卓手机上有用)
/*
bool ConnectDataBase()
{
ConnectPath = "URI = file:" + Application.persistentDataPath + "/IndividualEventDatabase.sqlite";
string androidPath = "jar:file://" + Application.dataPath + "!/assets/IndividualEventDatabase.sqlite";
string androidFilePath = Application.persistentDataPath + "/IndividualEventDatabase.sqlite";
//判断Application.persistentDataPath 下的IndividualEventDatabase数据库是否存在
if (!File.Exists(androidFilePath))
{
//如果不存在,就下载数据库
WWW www = new WWW(androidPath);
while (!www.isDone) { }
File.WriteAllBytes(androidFilePath, www.bytes);
}
//打开数据库
SqlCon = new SqliteConnection(ConnectPath);
if (SqlCon != null)
{
SqlCon.Open();
return true;
}
else
{
return false;
}
}
*/
//关闭数据库
private void CloseDataBase()
{
SqlCon.Close();
}
//增
public void AddDataBase(string sql)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = sql;
SqlCmd.ExecuteNonQuery();
CloseDataBase();
}
}
//删
public void Delete(string TableName)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = "delete from " + TableName;
SqlCmd.ExecuteNonQuery();
CloseDataBase();
}
}
//改
public void UpdateDataBase(string sql)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = sql;
SqlCmd.ExecuteNonQuery();
CloseDataBase();
}
}
//查
public object SelectDataBase(string sql)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = sql;
object obj = SqlCmd.ExecuteScalar();
CloseDataBase();
return obj;
}
else
{
return null;
}
}
public ArrayList SelectMonyDataBase(string Sql)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = Sql;
SqliteDataReader reader = SqlCmd.ExecuteReader();
ArrayList array = new ArrayList();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
array.Add(reader.GetValue(i));
}
}
CloseDataBase();
return array;
}
else
{
return null;
}
}
public string[] MonyStringData(string sql)
{
ArrayList array = SelectMonyDataBase(sql);
string[] name = new string[array.Count];
for (int i = 0; i < name.Length; i++)
{
name[i] = array[i].ToString();
}
return name;
}
public int[] CharacterInformation(string sql)
{
ArrayList arrayname = SelectMonyDataBase(sql);
int[] datainformation = new int[arrayname.Count];
for (int i = 0; i < datainformation.Length; i++)
{
datainformation[i] = int.Parse(arrayname[i].ToString());
}
return datainformation;
}
}
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;
using System.IO;
public class DataBasic {
//单例
private static DataBasic instance;
private DataBasic() { }
public static DataBasic Instance
{
get
{
if (instance == null)
{
instance = new DataBasic();
}
return instance;
}
}
private SqliteConnection SqlCon;
private string ConnectPath;
bool ConnectDataBase()
{
ConnectPath = "data source=" + Application.streamingAssetsPath + "/individualeventdatabase.sqlite";
SqlCon = new SqliteConnection(ConnectPath);
//判断是否存在数据库
if (SqlCon != null)
{
//如果存在,打来数据库
SqlCon.Open();
return true;
}
else
{
Debug.Log("数据库打开失败");
return false;
}
}
//判断是否连接数据库(此方法只在安卓手机上有用)
/*
bool ConnectDataBase()
{
ConnectPath = "URI = file:" + Application.persistentDataPath + "/IndividualEventDatabase.sqlite";
string androidPath = "jar:file://" + Application.dataPath + "!/assets/IndividualEventDatabase.sqlite";
string androidFilePath = Application.persistentDataPath + "/IndividualEventDatabase.sqlite";
//判断Application.persistentDataPath 下的IndividualEventDatabase数据库是否存在
if (!File.Exists(androidFilePath))
{
//如果不存在,就下载数据库
WWW www = new WWW(androidPath);
while (!www.isDone) { }
File.WriteAllBytes(androidFilePath, www.bytes);
}
//打开数据库
SqlCon = new SqliteConnection(ConnectPath);
if (SqlCon != null)
{
SqlCon.Open();
return true;
}
else
{
return false;
}
}
*/
//关闭数据库
private void CloseDataBase()
{
SqlCon.Close();
}
//增
public void AddDataBase(string sql)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = sql;
SqlCmd.ExecuteNonQuery();
CloseDataBase();
}
}
//删
public void Delete(string TableName)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = "delete from " + TableName;
SqlCmd.ExecuteNonQuery();
CloseDataBase();
}
}
//改
public void UpdateDataBase(string sql)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = sql;
SqlCmd.ExecuteNonQuery();
CloseDataBase();
}
}
//查
public object SelectDataBase(string sql)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = sql;
object obj = SqlCmd.ExecuteScalar();
CloseDataBase();
return obj;
}
else
{
return null;
}
}
public ArrayList SelectMonyDataBase(string Sql)
{
if (ConnectDataBase())
{
SqliteCommand SqlCmd = SqlCon.CreateCommand();
SqlCmd.CommandText = Sql;
SqliteDataReader reader = SqlCmd.ExecuteReader();
ArrayList array = new ArrayList();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
array.Add(reader.GetValue(i));
}
}
CloseDataBase();
return array;
}
else
{
return null;
}
}
public string[] MonyStringData(string sql)
{
ArrayList array = SelectMonyDataBase(sql);
string[] name = new string[array.Count];
for (int i = 0; i < name.Length; i++)
{
name[i] = array[i].ToString();
}
return name;
}
public int[] CharacterInformation(string sql)
{
ArrayList arrayname = SelectMonyDataBase(sql);
int[] datainformation = new int[arrayname.Count];
for (int i = 0; i < datainformation.Length; i++)
{
datainformation[i] = int.Parse(arrayname[i].ToString());
}
return datainformation;
}
}