public static RowData QuerySingle(this sqlite3 db, Query query) { var stmt = db.Prepare(query); try { for (var i = 0; i < query.Parameters.Count; i++) { stmt.BindParameter(query.Parameters[i], i + 1); } var result = SQLite.sqlite3_step(stmt); if (result == StepResult.Row) { return(new RowData(stmt, true)); } if (result == StepResult.Done) { return(null); } var err = SQLite.sqlite3_errmsg(db); throw new SQLiteException(err); } finally { stmt.Finalize(); } }
public static void Execute(this sqlite3 db, Query query) { if (query.Parameters == null || query.Parameters.Count == 0) { //Parameter less execute var result = SQLite.sqlite3_exec(db, query.Command); if (result != Error.OK) { var err = SQLite.sqlite3_errmsg(db); throw new SQLiteException(err); } } else { //With parameters to bind var stmt = db.Prepare(query); try { for (var i = 0; i < query.Parameters.Count; i++) { stmt.BindParameter(query.Parameters[i], i + 1); } var result = SQLite.sqlite3_step(stmt); if (result != StepResult.Done && result != StepResult.Row) { var err = SQLite.sqlite3_errmsg(db); throw new SQLiteException(err); } } finally { stmt.Finalize(); } } }
public static void QueryReader(this sqlite3 db, Query query, Action <RowData> onRow) { var stmt = db.Prepare(query); try { for (var i = 0; i < query.Parameters.Count; i++) { stmt.BindParameter(query.Parameters[i], i + 1); } RowData row = null; while (true) { var stepResult = SQLite.sqlite3_step(stmt); if (stepResult != StepResult.Row) { break; } if (row == null) { row = new RowData(stmt); } else { row.ClearForNewRow(); } onRow?.Invoke(row); } } finally { stmt.Finalize(); } }
public static void AddColumnIfNotExists(this sqlite3 db, string table, string column, string type) { var exists = TableColumnExists(db, table, column); if (exists) { return; } var query = $"ALTER TABLE {table} ADD COLUMN {column} {type}"; var stmt = db.Prepare(query); try { var result = SQLite.sqlite3_step(stmt); if (result != StepResult.Done) { var err = SQLite.sqlite3_errmsg(db); throw new SQLiteException("Cannot add column: " + err); } } finally { stmt?.Finalize(); } }