注:首先在Plugins文件夹下面引入三个dll文件mono.data.sqlite System.Data sqlite3
/*******************
********************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Mono.Data.Sqlite;
/// <summary>
/// 数据库操作的单例类
/// </summary>
public class DataBaseTool {
private static DataBaseTool _instance;//单例的静态引用
public static DataBaseTool Instance{
get{
if (_instance == null) {
_instance = new DataBaseTool ();
}
return _instance;
}
}
private string databaseName = "mydatabase.sqlite";//数据库名称
//数据库链接对象
private SqliteConnection sqlConnection = null;
//数据结果
private SqliteDataReader sqlDataReader = null;//
/// <summary>
/// 初始化数据库相关对象
/// </summary>
private DataBaseTool(){
//数据库链接地址
string path = "Data Source=" +
Application.streamingAssetsPath + "/" + databaseName;
if (sqlConnection==null) {
try{
sqlConnection = new SqliteConnection (path);
}catch(SqliteException e){
Debug.Log ("创建数据库链接失败...");
Debug.Log (e.ToString());
}
}
}
/// <summary>
/// 打开数据库链接
/// </summary>
private void OpenConnection(){
if (sqlConnection != null) {
try {
sqlConnection.Open ();
} catch (SqliteException e) {
Debug.Log ("打开数据库链接失败...");
Debug.Log (e.ToString ());
}
} else {
Debug.Log ("打开数据库链接失败...");
}
}
/// <summary>
/// 关闭数据库链接
/// </summary>
private void CloseConnection(){
if (sqlConnection != null) {
try {
sqlConnection.Close ();
} catch (SqliteException e) {
Debug.Log ("关闭数据库链接失败...");
Debug.Log (e.ToString ());
}
} else {
Debug.Log ("关闭数据库链接失败...");
}
}
/// <summary>
/// 执行增、删、改数据库操作
/// </summary>
public void ExcuteSql(string sqlStr){
OpenConnection ();
SqliteCommand sqlCommand = sqlConnection.CreateCommand ();
if (sqlCommand != null) {
sqlCommand.CommandText = sqlStr;
int result = sqlCommand.ExecuteNonQuery ();
} else {
Debug.Log ("执行数据库命令失败...");
}
CloseConnection ();
}
/// <summary>
/// 获得一行数据的方法
/// </summary>
public Dictionary<string , object> ExcuteOneClumeResult(string sql){
OpenConnection ();
Dictionary<string , object> result = new Dictionary<string, object> ();
SqliteCommand sqlCommand = sqlConnection.CreateCommand ();
if (sqlCommand != null) {
sqlCommand.CommandText = sql;
/**sqlDataReader里的内容:
HeroId HeroName AD AP Armor
1 EZ 10 0 0
**/
sqlDataReader = sqlCommand.ExecuteReader ();
while (sqlDataReader.Read ()) {
for (int i = 0; i < sqlDataReader.FieldCount; i++) {
result.Add (sqlDataReader.GetName (i), sqlDataReader.GetValue (i));
}
break;
}
} else {
result = null;
}
CloseConnection ();
return result;
}
/// <summary>
/// 返回查询的所有数据(多列)
/// </summary>
/// <returns>The all rresult.</returns>
public List<Dictionary<string,object>> ExcuteAllRresult(string sql){
OpenConnection ();
//存放查询的所有结果集
List<Dictionary<string,object>> results
= new List<Dictionary<string, object>> ();
SqliteCommand sqlCommand = sqlConnection.CreateCommand ();
if (sqlCommand != null) {
//打包sql语句
sqlCommand.CommandText = sql;
//执行sql语句并获得查询结果
sqlDataReader = sqlCommand.ExecuteReader ();
//逐行解析数据
while(sqlDataReader.Read()){
//单行数据的所有内容
Dictionary<string ,object> oneclum = new Dictionary<string, object> ();
for (int i = 0; i < sqlDataReader.FieldCount; i++) {
oneclum.Add (sqlDataReader.GetName(i),sqlDataReader.GetValue(i));
}
results.Add (oneclum);
}
}
CloseConnection ();
return results;
}
}