|
| 1 | +/** |
| 2 | + * SQLite Wrapper |
| 3 | + * |
| 4 | + * Author: Nicholas Dunnaway |
| 5 | + * Copyright: digitalroot.net 2008 - 2013 |
| 6 | + * Web: http://digitalroot.net |
| 7 | + * |
| 8 | + * This program was written to make reading and writing to a SQLite database easy. |
| 9 | + * |
| 10 | + * Requirements: |
| 11 | + * .Net Runtime Files |
| 12 | + * System.Data.SQLite.dll |
| 13 | + * |
| 14 | + */ |
| 15 | +using System; |
| 16 | +using System.IO; |
| 17 | +using System.Data; |
| 18 | +using System.Data.SQLite; |
| 19 | + |
| 20 | +namespace SQLiteWrapper |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// This class makes reading and writing to a SQLite database easy. |
| 24 | + /// </summary> |
| 25 | + public class SQLiteWrapper |
| 26 | + { |
| 27 | + /// <summary> |
| 28 | + /// Path used to locate the database file. |
| 29 | + /// </summary> |
| 30 | + private string _pathToDatabase; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Name of the database file. |
| 34 | + /// </summary> |
| 35 | + private readonly string _databaseFileName; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// This is the connection to the database. This holds it open |
| 39 | + /// so we do not have to reconnect each time we want to run a query. |
| 40 | + /// </summary> |
| 41 | + private static SQLiteConnection _cnn; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// This is what we use to issue commands to the database. |
| 45 | + /// </summary> |
| 46 | + private static SQLiteCommand _cmd; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Public method for getting and settings the Path to the database file. |
| 50 | + /// </summary> |
| 51 | + public string PathToDatabase |
| 52 | + { |
| 53 | + get { return _pathToDatabase; } |
| 54 | + set |
| 55 | + { |
| 56 | + if (Directory.Exists(value)) |
| 57 | + { |
| 58 | + char[] charsToTrim = { '\\', '/' }; // Remove trailing slash. |
| 59 | + _pathToDatabase = value.TrimEnd(charsToTrim); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + throw new Exception("Path for database is invalid."); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// This creates the database for the first time. It creates a by default a settings table. |
| 70 | + /// </summary> |
| 71 | + private void CreateSettingsTable() |
| 72 | + { |
| 73 | + using (_cnn = new SQLiteConnection("Data Source=" + _pathToDatabase + '\\' + _databaseFileName)) |
| 74 | + { |
| 75 | + using (_cmd = _cnn.CreateCommand()) |
| 76 | + { |
| 77 | + _cnn.Open(); // Open the database connection. |
| 78 | + |
| 79 | + // Create a new table |
| 80 | + _cmd.CommandText = "CREATE TABLE settings (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(150) UNIQUE, value VARCHAR(250))"; |
| 81 | + _cmd.ExecuteNonQuery(); // Create the table, don't expect returned data |
| 82 | + |
| 83 | + // Insert something into the table |
| 84 | + _cmd.CommandText = "INSERT INTO settings (name, value) VALUES ('dbInstalled', 'TRUE')"; |
| 85 | + _cmd.ExecuteNonQuery(); |
| 86 | + |
| 87 | + // Read the values back out |
| 88 | + _cmd.CommandText = "SELECT value FROM settings WHERE name = 'dbInstalled'"; |
| 89 | + using (SQLiteDataReader reader = _cmd.ExecuteReader()) |
| 90 | + { |
| 91 | + while (reader.Read()) |
| 92 | + { |
| 93 | + //Console.WriteLine(String.Format("id = {0}, ServerName = {1}", reader[0], reader[1])); |
| 94 | + if (reader[0].ToString() != "TRUE") |
| 95 | + { |
| 96 | + // Database was not setup correctly. We should prob delete the database file. |
| 97 | + File.Delete(_pathToDatabase + '\\' + _databaseFileName); |
| 98 | + throw new Exception("Error creating database"); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// This queries the database and returns the values of a setting. |
| 108 | + /// </summary> |
| 109 | + /// <param name="name">Name of the settings you would like the value for.</param> |
| 110 | + public string Get(string name) |
| 111 | + { |
| 112 | + using (_cmd = _cnn.CreateCommand()) |
| 113 | + { |
| 114 | + // check if the connection is already open. If not Open it. |
| 115 | + if (_cnn.State != ConnectionState.Open) |
| 116 | + { |
| 117 | + _cnn.Open(); |
| 118 | + } |
| 119 | + |
| 120 | + // Read the values back out |
| 121 | + _cmd.CommandText = "SELECT value FROM settings WHERE name = '" + name + "'"; |
| 122 | + using (SQLiteDataReader reader = _cmd.ExecuteReader()) |
| 123 | + { |
| 124 | + while (reader.Read()) |
| 125 | + { |
| 126 | + return reader[0].ToString(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return ""; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// This is for passing raw commands to the database. |
| 135 | + /// </summary> |
| 136 | + /// <param name="sqlCommand">SQL Query</param> |
| 137 | + /// <returns>True or throws an Exception</returns> |
| 138 | + public bool RawCmd(string sqlCommand) |
| 139 | + { |
| 140 | + try |
| 141 | + { |
| 142 | + using (_cmd = _cnn.CreateCommand()) |
| 143 | + { |
| 144 | + // check if the connection is already open. If not Open it. |
| 145 | + if (_cnn.State != ConnectionState.Open) |
| 146 | + { |
| 147 | + _cnn.Open(); |
| 148 | + } |
| 149 | + |
| 150 | + // Run command from user |
| 151 | + _cmd.CommandText = sqlCommand; |
| 152 | + _cmd.ExecuteNonQuery(); |
| 153 | + } |
| 154 | + } |
| 155 | + catch (Exception e) |
| 156 | + { |
| 157 | + throw new Exception(e.Message); |
| 158 | + } |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// This is for passing raw select statements to the database. |
| 164 | + /// </summary> |
| 165 | + /// <param name="sqlCommand">SQL Query</param> |
| 166 | + /// <returns>True or throws an Exception</returns> |
| 167 | + public DataTable Select(string sqlCommand) |
| 168 | + { |
| 169 | + using (_cmd = _cnn.CreateCommand()) |
| 170 | + { |
| 171 | + // check if the connection is already open. If not Open it. |
| 172 | + if (_cnn.State != ConnectionState.Open) |
| 173 | + { |
| 174 | + _cnn.Open(); |
| 175 | + } |
| 176 | + |
| 177 | + // Read the values back out |
| 178 | + var myTable = new DataTable(); |
| 179 | + |
| 180 | + using (var myDataAdp = new SQLiteDataAdapter(sqlCommand, _cnn)) |
| 181 | + { |
| 182 | + using (new SQLiteCommandBuilder(myDataAdp)) |
| 183 | + { |
| 184 | + myDataAdp.Fill(myTable); |
| 185 | + } |
| 186 | + } |
| 187 | + return myTable; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// Save the value of something to the database so we can get it back later. |
| 193 | + /// </summary> |
| 194 | + /// <param name="name">Name of the settings you would like to set.</param> |
| 195 | + /// <param name="value">Value you of settings we are saving.</param> |
| 196 | + /// <returns>Returns True if the value was saved to the database.</returns> |
| 197 | + public bool Set(string name, string value) |
| 198 | + { |
| 199 | + try |
| 200 | + { |
| 201 | + using (_cmd = _cnn.CreateCommand()) |
| 202 | + { |
| 203 | + // check if the connection is already open. If not Open it. |
| 204 | + if (_cnn.State != ConnectionState.Open) |
| 205 | + { |
| 206 | + _cnn.Open(); |
| 207 | + } |
| 208 | + |
| 209 | + // Insert something into the table |
| 210 | + _cmd.CommandText = "DELETE FROM settings WHERE name = '" + name + "'"; |
| 211 | + _cmd.ExecuteNonQuery(); |
| 212 | + |
| 213 | + // Insert something into the table |
| 214 | + _cmd.CommandText = "INSERT INTO settings (name, value) VALUES ('" + name + "', '" + value + "')"; |
| 215 | + _cmd.ExecuteNonQuery(); |
| 216 | + } |
| 217 | + } |
| 218 | + catch (Exception e) |
| 219 | + { |
| 220 | + throw new Exception(e.Message); |
| 221 | + } |
| 222 | + return true; |
| 223 | + } |
| 224 | + |
| 225 | + /// <summary> |
| 226 | + /// Constructor |
| 227 | + /// </summary> |
| 228 | + /// <param name="databaseName">Database File Name</param> |
| 229 | + /// <param name="pathToDatabase">Path to the database file.</param> |
| 230 | + public SQLiteWrapper(string databaseName, string pathToDatabase) |
| 231 | + { |
| 232 | + _pathToDatabase = pathToDatabase; // Set the path |
| 233 | + _databaseFileName = databaseName; // Set File Name |
| 234 | + if (File.Exists(_pathToDatabase + '\\' + _databaseFileName) == false) |
| 235 | + { |
| 236 | + CreateSettingsTable(); |
| 237 | + } |
| 238 | + else |
| 239 | + { |
| 240 | + using (_cnn = new SQLiteConnection("Data Source=" + _pathToDatabase + '\\' + _databaseFileName)) |
| 241 | + { |
| 242 | + using (_cmd = _cnn.CreateCommand()) |
| 243 | + { |
| 244 | + _cnn.Open(); // Open the database connection. |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + /// <summary> |
| 251 | + /// Constructor |
| 252 | + /// </summary> |
| 253 | + /// <param name="databaseName">Database File Name</param> |
| 254 | + public SQLiteWrapper(string databaseName) |
| 255 | + : this(databaseName, ".") |
| 256 | + { } |
| 257 | + } |
| 258 | +} |
0 commit comments