using IOTContainer.SQLite; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IOTContainer.Common { public class LocalStorage { /// /// Sqlite数据库路径 /// private static string _dbFilePath = AppDomain.CurrentDomain.BaseDirectory + @"\QMSql\localstorage.qm"; /// /// 检查数据库文件是否存在 /// public static void CheckDBFile() { if (!File.Exists(_dbFilePath)) { if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\QMSql")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\QMSql"); } var stream = File.Create(_dbFilePath); stream.Close(); stream.Dispose(); CreateInfoTable(); Log.MyLog.WriteLogFile("数据库文件不存在" + Environment.NewLine + _dbFilePath, "SqliteLog"); } } /// /// 创建Info表 /// public static bool CreateInfoTable() { try { using (var adapter = new SQLiteAdapter(_dbFilePath, null)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { actuator.Execute("CREATE TABLE infos(key text primary key ,value text)"); Log.MyLog.WriteLogFile("成功创建Info表" + Environment.NewLine + _dbFilePath, "SqliteLog"); } } return true; } catch (Exception ex) { Log.MyLog.WriteLogFile("创建Info表错误" + Environment.NewLine + ex.ToString(), "SqliteLogError"); return false; } } /// /// 创建项目表 /// /// 项目名称 public static bool CreatePipTable(string pipelineName) { try { if (!IsHasTable(pipelineName)) { using (var adapter = new SQLiteAdapter(_dbFilePath, null)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { actuator.Execute("CREATE TABLE " + pipelineName + "(key text primary key ,value text)"); Log.MyLog.WriteLogFile("成功创建" + pipelineName + "表", "SqliteLog"); } } return true; } else { Log.MyLog.WriteLogFile("创建失败存在表 tableName:" + pipelineName, "SqliteLog"); return false; } } catch (Exception ex) { Log.MyLog.WriteLogFile("创建" + pipelineName + "表错误" + Environment.NewLine + ex.ToString(), "SqliteLogError"); return false; } } /// /// 插入数据 /// /// 表名 /// 主键 /// 值 public static bool InsertPipTable(string pipelineName, string key, string value) { try { if (!IsHasPrimaryKey(pipelineName, key)) { using (var adapter = new SQLiteAdapter(_dbFilePath)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { var parameters = new Dictionary(); parameters.Add("key", key); parameters.Add("value", value); actuator.Insert(pipelineName, parameters); } } Log.MyLog.WriteLogFile("成功插入" + pipelineName + "表," + "key:" + key + "value:" + value, "SqliteLog"); return true; } else { using (var adapter = new SQLiteAdapter(_dbFilePath)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { var parameters = new Dictionary(); parameters.Add("value", value); actuator.Update(pipelineName, parameters, "key", key); } } Log.MyLog.WriteLogFile("成功更新" + pipelineName + "表," + "key:" + key + "value:" + value, "SqliteLog"); return true; //Log.MyLog.WriteLogFile("插入" + pipelineName + "表," + "key:" + key + "value:" + value + "失败,主键存在", "SqliteLog"); //return false; ; } } catch (Exception ex) { Log.MyLog.WriteLogFile("插入" + pipelineName + "表," + "key:" + key + "value:" + value + "失败" + Environment.NewLine + ex.ToString(), "SqliteLogError"); return false; } } /// /// 是否存在主键 /// /// 表名 /// 主键 /// public static bool IsHasPrimaryKey(string tableName, string key) { try { using (var adapter = new SQLiteAdapter(_dbFilePath)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { using (var result = actuator.Select("select value from " + tableName + " where key='" + key + "'")) { if (result.Rows.Count > 0) return true; else return false; } } } } catch (Exception ex) { Log.MyLog.WriteLogFile("判断主键是否唯一错误 table:" + tableName + "key:" + key + Environment.NewLine + ex.ToString(), "SqliteLogError"); return false; } } /// /// 查找 /// /// 项目名称 /// 主键 /// public static string SelectPipTable(string pipelineName, string key) { try { using (var adapter = new SQLiteAdapter(_dbFilePath)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { using (var result = actuator.Select("select value from " + pipelineName + " where key='" + key + "'")) { Log.MyLog.WriteLogFile("查找成功 table:" + pipelineName + "key:" + key + "value:" + result, "SqliteLog"); return result.Rows[0]["value"].ToString(); } } } } catch (Exception ex) { Log.MyLog.WriteLogFile("查找异常 table:" + pipelineName + "key:" + key + Environment.NewLine + ex.ToString(), "SqliteLogError"); return ""; } } /// /// 查找表内全数据 /// /// 项目名称 /// public static DataTable SelectPipTable(string pipelineName) { try { using (var adapter = new SQLiteAdapter(_dbFilePath)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { var result = actuator.Select("select * from " + pipelineName); Log.MyLog.WriteLogFile("查找成功 table:" + pipelineName, "SqliteLog"); return result; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("查找异常 table:" + pipelineName + Environment.NewLine + ex.ToString(), "SqliteLogError"); return null; } } public static string SelectH5Ve() { try { using (var adapter = new SQLiteAdapter(_dbFilePath)) { string ver = string.Empty; adapter.Connection(); using (var actuator = adapter.GetActuator()) { using (var result = actuator.Select("select * from H5Ve")) { Log.MyLog.WriteLogFile("查找H5Ve成功 ", "SqliteLog"); for (int i = 0; i < result.Rows.Count; i++) { if (i == result.Rows.Count - 1) { string key = result.Rows[i]["key"].ToString(); string value = result.Rows[i]["value"].ToString(); ver = ver + "\"" + key + "\":\"" + value + "\""; } else { string key = result.Rows[i]["key"].ToString(); string value = result.Rows[i]["value"].ToString(); ver = ver + "\"" + key + "\":\"" + value + "\","; } } return "{" + ver + " }"; } } } } catch (Exception ex) { Log.MyLog.WriteLogFile("查找异常 H5Ve" + Environment.NewLine + ex.ToString(), "SqliteLogError"); return ""; } } /// /// 删除 /// /// 项目名称 /// 主键 public static bool DelPipRow(string pipelineName, string key) { try { using (var adapter = new SQLiteAdapter(_dbFilePath)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { actuator.Execute("delete from " + pipelineName + " where key ='" + key + "'"); Log.MyLog.WriteLogFile("删除成功 table:" + pipelineName + "key:" + key, "SqliteLog"); return true; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("删除异常 table:" + pipelineName + "key:" + key + Environment.NewLine + ex.ToString(), "SqliteLogError"); return false; } } /// /// 是否存在表 /// /// /// public static bool IsHasTable(string tableName) { try { using (var adapter = new SQLiteAdapter(_dbFilePath)) { adapter.Connection(); using (var actuator = adapter.GetActuator()) { var result = actuator.Select("SELECT COUNT(*) FROM sqlite_master where type='table' and name='" + tableName + "'"); var count = Convert.ToInt32(result.Rows[0]["COUNT(*)"]); Log.MyLog.WriteLogFile("tablename:" + tableName + "是否存在:" + count.ToString(), "SqliteLog"); if (count == 0) return false; else return true; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("判断存在表异常 tablename:" + tableName + Environment.NewLine + ex.ToString(), "SqliteLogError"); return false; } } public static void DropData() { } } }