赞
踩
using Mono.Data.Sqlite; using System; using System.IO; using System.Collections.Generic; using UnityEngine; public class SQLManager { //建立数据库连接 private static SqliteConnection connection; //数据库命令 private static SqliteCommand command; //数据库阅读器 private static SqliteDataReader reader; public SQLManager(string dbPath,string dbName) { //连接数据库 OpenConnect(dbPath, dbName); } /// <summary> /// 打开数据库 /// </summary> public void OpenConnect(string dbPath, string dbName) { try { if (!Directory.Exists(dbPath)) { Directory.CreateDirectory(dbPath); } //数据库存放在 Asset/StreamingAssets string path = dbPath + dbName + ".db"; //新建数据库连接 connection = new SqliteConnection(@"Data Source = " + path); //打开数据库 connection.Open(); Debug.Log("打开数据库"); } catch (Exception ex) { Debug.Log(ex.ToString()); } } /// <summary> /// 关闭数据库 /// </summary> public void CloseDB() { if (command != null) { command.Cancel(); } command = null; if (reader != null) { reader.Close(); } reader = null; if (connection != null) { //connection.Close(); } connection = null; Debug.Log("关闭数据库"); } /// <summary> /// 插入数据 /// </summary> /// <param name="tableName"></param> /// <param name="values"></param> /// <returns></returns> public bool InsertValues(string tableName, List<string> values) { try { string sql = "INSERT INTO " + tableName + " values ("; foreach (var item in values) { sql += "'" + item + "',"; } sql = sql.TrimEnd(',') + ")"; ExecuteQuery(sql); } catch (Exception e) { Debug.Log(e.ToString()); } return true; } /// <summary> /// 删除数据 /// </summary> /// <param name="tableName"></param> /// <param name="conditions">查询条件</param> /// <returns></returns> public SqliteDataReader DeleteValues(string tableName, List<string> conditions) { string sql = "delete from " + tableName + " where ("; try { for (int i = 0; i < conditions.Count - 1; i += 2) { sql += conditions[i] + "='" + conditions[i + 1] + "' and "; } sql = sql.Substring(0, sql.Length - 4) + ");"; } catch (Exception e) { Debug.Log(e.ToString()); } return ExecuteQuery(sql); } /// <summary> /// 更新数据 /// </summary> /// <param name="tableName"></param> /// <param name="values">需要修改的数据</param> /// <param name="conditions">修改的条件</param> /// <returns></returns> public SqliteDataReader UpdataData(string tableName, List<string> values, List<string> conditions) { string sql = "update " + tableName + " set "; for (int i = 0; i < values.Count - 1; i += 2) { sql += values[i] + "='" + values[i + 1] + "',"; } sql = sql.TrimEnd(',') + " where ("; for (int i = 0; i < conditions.Count - 1; i += 2) { sql += conditions[i] + "='" + conditions[i + 1] + "' and "; } sql = sql.Substring(0, sql.Length - 4) + ");"; Debug.Log("更新成功"); return ExecuteQuery(sql); } /// <summary> /// 查询数据 /// </summary> /// <param name="tableName">表名</param> /// <returns></returns> public List<string> GetDataBySqlQuery(string tableName) { List<string> list = new List<string>(); try { string queryString = "SELECT * FROM " + tableName; reader = ExecuteQuery(queryString); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { object obj = reader.GetValue(i); list.Add(obj.ToString()); } } } catch (Exception e) { Debug.Log(e.ToString()); } return list; } /// <summary> /// 执行SQL命令 /// </summary> /// <param name="queryString">SQL命令字符串</param> /// <returns></returns> private SqliteDataReader ExecuteQuery(string queryString) { command = connection.CreateCommand(); command.CommandText = queryString; reader = command.ExecuteReader(); return reader; } /// <summary> /// 删除表 /// </summary> /// <param name="tableName"></param> /// <returns></returns> public SqliteDataReader DeleteTable(string tableName) { string sql = "DROP TABLE " + tableName; return ExecuteQuery(sql); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。