赞
踩
这里在写了一个SqlManager类的单例来实现Sql方法在Unity中的使用,Sql语句基本的增删查改语言在我的另外一篇博客里面有记录( Sql安装及Sql增删查改)https://blog.csdn.net/weixin_43492764/article/details/88393806
代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using Mono.Data.Sqlite; using System; public class SqlManager { #region 单例模式 //SqlManager的一个单例 private static SqlManager instance; public static SqlManager GetInstance() { if (instance == null) { instance = new SqlManager(); } return instance; } private SqlManager() { } #endregion #region sqlite数据库的一些基础操作方法的封装 /// <summary> /// Sqlite数据库的一些基础操作的封装 /// </summary> private SqliteConnection connect;//数据连接对象 private SqliteCommand command;//sql指令对象 private SqliteDataReader reader;//数据读取对象 /// <summary> /// 打开Sqlite数据库连接的方法 /// </summary> ///<param name="dataBaseName">数据库名称</param> public void OpenDataBase(string dataBaseName) { if (!dataBaseName.Contains(".sqlite")) { dataBaseName += ".sqlite"; } //数据库路径拼接 string类型 string dataPath = "Data Source =" + Application.streamingAssetsPath +"/"+ dataBaseName; //对数据库连接对象进行赋值 connect = new SqliteConnection(dataPath); //对sql指令对象进行赋值 command = connect.CreateCommand(); //打开数据路连接 connect.Open(); } /// <summary> /// 关闭数据路 /// </summary> public void CloseDataBase() { try { connect.Close(); if (reader != null) { reader.Close(); } } catch (SqliteException ex) { Debug.LogError(ex.ToString()); } } /// <summary> /// 执行非查询的工作(增删改三个不需要返回值的工作) /// </summary> ///<param name="sql">sql指令</param> public void RunNoneQuery(string sql) { try { command.CommandText = sql; command.ExecuteNonQuery(); } catch (SqliteException ex) { Debug.Log(ex.ToString()); } } /// <summary> /// 查询单个数据 /// </summary> /// <param name="sql">sql指令</param> /// <returns></returns> public object SelectSingleData(string sql) { try { command.CommandText = sql; object obj = command.ExecuteScalar(); return obj; } catch (SqliteException ex) { Debug.Log(ex.ToString()); return null; } } /// <summary> /// 查询多个数据 /// </summary> /// <param name="sql">sql指令</param> /// <returns></returns> public List<ArrayList> SelectMutidata(string sql) { try { List<ArrayList> result = new List<ArrayList>(); command.CommandText = sql; reader = command.ExecuteReader(); while (reader.Read()) { ArrayList temp = new ArrayList(); for (int i = 0; i < reader.FieldCount; i++) { temp.Add(reader.GetValue(i)); } result.Add(temp); } reader.Close(); return result; } catch (SqliteException ex) { Debug.Log(ex.ToString()); return null; } } #endregion #region 当前项目的一些常用方法的封装 #endregion }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。