SqlLiteHelper内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
using UnityEngine; using System.Collections; using Mono.Data.Sqlite; using System.Text; /// <summary> /// 字段类型 /// </summary> public static class SqlLiteHelper { public static SqliteConnection dbConnection; public static SqliteCommand dbCommand; public static string conStr = "Data Source=" + Application.dataPath + "/Data/data.db" ; /// <summary> /// 默认构造函数打开数据库 创建数据库指令 /// </summary> public static void Initialize(){ dbConnection = new SqliteConnection (conStr); dbConnection.Open(); dbCommand = dbConnection.CreateCommand(); } public static void Create(string _TableName, string[] _ColsName, string [] _ColsType) { try { string query = "create table " + _TableName + " (" + _ColsName[0] + " " + _ColsType[0]; for (int i = 1; i < _ColsName.Length; i++) { query += ", " + _ColsName[i] + " " + _ColsType[i]; } query += ")"; ExecuteQuery(query); } catch { Debug.Log("创建失败" ); } } /// <summary> /// 插入数据 /// 参数:表名称、值 /// 特点:不适用自动ID /// </summary> public static void Insert(string _TableName, string[] _Values) { try { string query = "insert into " + _TableName + " values(" + _Values[0]; for (int i = 1; i < _Values.Length; i++) { query += "," + _Values[i]; } query += ")"; ExecuteQuery(query); } catch { Debug.Log("添加失败" ); } } /// <summary> /// 修改数据 /// 参数:表名称、准备修改的属性列、准备修改的属性列的值、条件信息属性列、条件信息属性列的值 /// </summary> public static void Update(string _TableName, string[] _SetColsName, string [] _SetColsValues, string _ConditionColName, string _ConditionColValue) { try { string query = "update " + _TableName + " set " + _SetColsName[0] + " = " + _SetColsValues[0]; for (int i = 1; i < _SetColsValues.Length; i++) { query += "," + _SetColsName[i] + " = " + _SetColsValues[i]; } query += " where " + _ConditionColName + " = " + _ConditionColValue; ExecuteQuery(query); } catch { Debug.Log("修改失败" ); } } /// <summary> /// 查询数据 /// 参数:表名称、查询内容 /// </summary> public static SqliteDataReader Select(string _TableName, string _Select) { try { string query = "select " + _Select + " from " + _TableName; dbCommand.CommandText = query; dbCommand.ExecuteNonQuery(); return dbCommand.ExecuteReader(); } catch { Debug.Log("查询失败" ); return null ; } } /// <summary> /// 查询数据 /// 参数:表名称、查询内容、查询条件 /// </summary> public static SqliteDataReader Select(string _TableName, string _Select, string _Condition) { try { string query = "select " + _Select + " from " + _TableName + " where " + _Condition; dbCommand.CommandText = query; dbCommand.ExecuteNonQuery(); return dbCommand.ExecuteReader(); } catch { Debug.Log("查询失败" ); return null ; } } /// <summary> /// 删除数据 /// 参数:表名称、属性列、值 /// </summary> public static void Delete(string _TableName, string _ColName, string _ColValue) { try { string query = "delete from " + _TableName + " where " + _ColName + " = " + _ColValue; ExecuteQuery(query); } catch { Debug.Log("删除失败" ); } } /// <summary> /// 关闭数据库连接 /// </summary> public static void Close() { try { dbConnection.Close(); } catch { Debug.Log("关闭失败" ); } } public static void ExecuteQuery(string sqlQuery) { dbCommand.CommandText = sqlQuery; dbCommand.ExecuteNonQuery(); } } |
- 本文固定链接: http://www.u3d8.com/?p=146
- 转载请注明: 网虫虫 在 u3d8.com 发表过