| using Mono.Data.Sqlite; |
| using System; |
| using System.Text; |
| using UnityEngine; |
| |
| namespace Common |
| { |
| |
| |
| |
| public class SqliteHelper |
| { |
| private SqliteConnection dbConnection; |
| |
| private SqliteCommand dbCommand; |
| |
| private SqliteDataReader reader; |
| |
| private StringBuilder strBuilder; |
| |
| public SqliteHelper() |
| { |
| strBuilder = new StringBuilder(); |
| } |
| |
| |
| |
| |
| public SqliteHelper(string path) |
| { |
| strBuilder = new StringBuilder(); |
| OpenDB(path); |
| } |
| |
| |
| |
| |
| public void OpenDB(string path) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("URI=file:"); |
| strBuilder.Append(PathHelper.GetPath(path)); |
| |
| try |
| { |
| dbConnection = new SqliteConnection(strBuilder.ToString()); |
| dbConnection.Open(); |
| } |
| catch (Exception e) |
| { |
| Debug.LogError(e.ToString()); |
| } |
| } |
| |
| |
| |
| |
| public void CloseSqlConnection() |
| { |
| dbCommand?.Dispose(); |
| dbCommand = null; |
| |
| reader?.Dispose(); |
| reader = null; |
| |
| dbConnection?.Close(); |
| dbConnection = null; |
| } |
| |
| |
| |
| |
| private SqliteDataReader Execute(string sqlQuery) |
| { |
| dbCommand = dbConnection.CreateCommand(); |
| |
| dbCommand.CommandText = sqlQuery; |
| |
| reader = dbCommand.ExecuteReader(); |
| |
| return reader; |
| } |
| |
| |
| |
| |
| public SqliteDataReader ReadFullTable(string tableName) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("SELECT * FROM "); |
| strBuilder.Append(tableName); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader InsertInto(string tableName, string[] values) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("INSERT INTO "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" VALUES ("); |
| strBuilder.Append(values[0]); |
| |
| for (int i = 1; i < values.Length; ++i) |
| { |
| strBuilder.Append(", "); |
| strBuilder.Append(values[i]); |
| } |
| |
| strBuilder.Append(")"); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader InsertInto(string tableName, string[] cols, string[] values) |
| { |
| if (cols.Length != values.Length) |
| { |
| throw new SqliteException("columns.Length != values.Length"); |
| } |
| |
| strBuilder.Clear(); |
| |
| strBuilder.Append("INSERT INTO "); |
| strBuilder.Append(tableName); |
| strBuilder.Append("("); |
| strBuilder.Append(cols[0]); |
| |
| for (int i = 1; i < cols.Length; ++i) |
| { |
| strBuilder.Append(", "); |
| strBuilder.Append(cols[i]); |
| } |
| |
| strBuilder.Append(") VALUES ("); |
| strBuilder.Append(values[0]); |
| |
| for (int i = 1; i < values.Length; ++i) |
| { |
| strBuilder.Append(", "); |
| strBuilder.Append(values[i]); |
| } |
| |
| strBuilder.Append(")"); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader UpdateInto(string tableName, string selectkey, string selectvalue, string[] cols, string[] colsvalues) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("UPDATE "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" SET "); |
| strBuilder.Append(cols[0]); |
| strBuilder.Append(" = "); |
| strBuilder.Append(colsvalues[0]); |
| |
| for (int i = 1; i < colsvalues.Length; ++i) |
| { |
| strBuilder.Append(", "); |
| strBuilder.Append(cols[i]); |
| strBuilder.Append(" = "); |
| strBuilder.Append(colsvalues[i]); |
| } |
| |
| strBuilder.Append(" WHERE "); |
| strBuilder.Append(selectkey); |
| strBuilder.Append(" = "); |
| strBuilder.Append(selectvalue); |
| strBuilder.Append(" "); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader Delete(string tableName, string[] cols, string[] colsvalues) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("DELETE FROM "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" WHERE "); |
| strBuilder.Append(cols[0]); |
| strBuilder.Append(" = "); |
| strBuilder.Append(colsvalues[0]); |
| |
| for (int i = 1; i < colsvalues.Length; ++i) |
| { |
| strBuilder.Append(" or "); |
| strBuilder.Append(cols[i]); |
| strBuilder.Append(" = "); |
| strBuilder.Append(colsvalues[i]); |
| } |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader DeleteContents(string tableName) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("DELETE FROM "); |
| strBuilder.Append(tableName); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader CreateTable(string tableName, string[] col, string[] colType, bool primaryKey_FirstField) |
| { |
| if (col.Length != colType.Length) |
| { |
| throw new SqliteException("The number of fields is not the same as the number of field types"); |
| } |
| |
| strBuilder.Clear(); |
| |
| strBuilder.Append("CREATE TABLE "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" ("); |
| strBuilder.Append(col[0]); |
| strBuilder.Append(" "); |
| strBuilder.Append(colType[0]); |
| |
| if (primaryKey_FirstField) |
| { |
| strBuilder.Append(" PRIMARY KEY NOT NULL"); |
| } |
| |
| for (int i = 1; i < col.Length; ++i) |
| { |
| strBuilder.Append(", "); |
| strBuilder.Append(col[i]); |
| strBuilder.Append(" "); |
| strBuilder.Append(colType[i]); |
| } |
| |
| strBuilder.Append(")"); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values, bool isAnd) |
| { |
| if (col.Length != operation.Length || operation.Length != values.Length) |
| { |
| throw new SqliteException("col.Length != operation.Length != values.Length"); |
| } |
| |
| strBuilder.Clear(); |
| |
| strBuilder.AppendLine("SELECT "); |
| strBuilder.AppendLine(items[0]); |
| |
| for (int i = 1; i < items.Length; ++i) |
| { |
| strBuilder.Append(", "); |
| strBuilder.Append(items[i]); |
| } |
| |
| strBuilder.Append(" FROM "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" WHERE "); |
| strBuilder.Append(col[0]); |
| strBuilder.Append(operation[0]); |
| strBuilder.Append("'"); |
| strBuilder.Append(values[0]); |
| strBuilder.Append("' "); |
| |
| if (isAnd) |
| { |
| for (int i = 1; i < col.Length; ++i) |
| { |
| strBuilder.Append("AND "); |
| strBuilder.Append(col[i]); |
| strBuilder.Append(operation[i]); |
| strBuilder.Append("'"); |
| strBuilder.Append(values[i]); |
| strBuilder.Append("' "); |
| } |
| } |
| else |
| { |
| for (int i = 1; i < col.Length; ++i) |
| { |
| strBuilder.Append("OR "); |
| strBuilder.Append(col[i]); |
| strBuilder.Append(operation[i]); |
| strBuilder.Append("'"); |
| strBuilder.Append(values[i]); |
| strBuilder.Append("' "); |
| } |
| } |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader Select(string tableName, string col) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("SELECT "); |
| strBuilder.Append(col); |
| strBuilder.Append(" FROM "); |
| strBuilder.Append(tableName); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader Select(string tableName, string col, string values) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("SELECT * FROM "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" WHERE "); |
| strBuilder.Append(col); |
| strBuilder.Append(" = "); |
| strBuilder.Append(values); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader Select(string tableName, string col, string operation, string values) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("SELECT * FROM "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" WHERE "); |
| strBuilder.Append(col); |
| strBuilder.Append(operation); |
| strBuilder.Append(values); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader SelectOrderASC(string tableName, string col) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("SELECT * FROM "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" ORDER BY "); |
| strBuilder.Append(col); |
| strBuilder.Append(" ASC"); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader SelectOrderDESC(string tableName, string col) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("SELECT * FROM "); |
| strBuilder.Append(tableName); |
| strBuilder.Append(" ORDER BY "); |
| strBuilder.Append(col); |
| strBuilder.Append(" DESC"); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader SelectCount(string tableName) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("SELECT COUNT(*) FROM "); |
| strBuilder.Append(tableName); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader SelectTableName() |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("SELECT name FROM sqlite_master WHERE type='table'"); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| |
| |
| |
| public SqliteDataReader SelectTableInfo(string tableName) |
| { |
| strBuilder.Clear(); |
| |
| strBuilder.Append("PRAGMA table_info("); |
| strBuilder.Append(tableName); |
| strBuilder.Append(")"); |
| |
| return Execute(strBuilder.ToString()); |
| } |
| |
| } |
| } |