using System; using System.Data.SQLite; namespace Mk0.Tools.SQLiteTools { public static class SQLiteTools { /// /// Connects to SQLite, only if sql=null /// /// /// /// public static SQLiteConnection SQLiteConnect(this SQLiteConnection sql, string fileName) { if (sql == null) { try { sql = new SQLiteConnection("Data Source=" + fileName + ";Version=3;") { ParseViaFramework = true }; sql.Open(); } catch (Exception ex) { throw new SQLiteToolsException("Not connected to SQLite.", ex); } } return sql; } /// /// Disconnects from SQLite (optional VACUUM before diconnection) /// /// /// public static void SQLiteDisconnect(this SQLiteConnection sql, bool vacuumBeforeClose = false) { if (sql != null) { if (vacuumBeforeClose) { try { SQLiteExecute(sql, "VACUUM;"); } catch (Exception ex) { throw new SQLiteToolsException("Could not VACUUM before closing the SQLite Connection.", ex); } } try { sql.Close(); sql.Dispose(); } catch (Exception ex) { throw new SQLiteToolsException("Error while closing SQLite Connection.", ex); } } else { throw new SQLiteToolsException("SQLite Connection is NULL - so it could not be closed."); } } /// /// Executes Queries on SQLite (no selects!) /// /// /// public static void SQLiteExecute(this SQLiteConnection sql, string query) { try { SQLiteCommand command = new SQLiteCommand(sql) { CommandText = query }; command.ExecuteNonQuery(); command.Dispose(); } catch (Exception ex) { throw new SQLiteToolsException("Could not execute the Query \"" + query + "\".", ex); } } } }