Skip to content

Instantly share code, notes, and snippets.

@mattiamerlini
Created December 23, 2015 09:26
Show Gist options
  • Save mattiamerlini/9f9e015ff66c700061d6 to your computer and use it in GitHub Desktop.
Save mattiamerlini/9f9e015ff66c700061d6 to your computer and use it in GitHub Desktop.
Database Manager for Android
package merlini.mvcpattern.Database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by mattia on 16/12/15.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static String CREATE_DATABASE =
"CREATE TABLE Utenti" +
"(" +
"id INT PRIMARY KEY," +
"nome VARCHAR(300) NOT NULL," +
"cognome VARCHAR(300) NOT NULL," +
"email VARCHAR(300) NOT NULL," +
"psw VARCHAR(300) NOT NULL" +
")";
private String dbName;
public DatabaseHelper(Context context, String dbName) {
super(context, dbName, null, 1);
this.dbName = dbName;
//context.deleteDatabase(dbName);
}
@Override
public void onCreate(SQLiteDatabase db) {
//Se all'avvio non trova nessun database già creato
db.execSQL(DatabaseHelper.CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package merlini.mvcpattern.Database;
import android.content.ContentValues;
import android.database.Cursor;
/**
* Created by mattia on 23/12/15.
*/
public abstract class QueryManager {
private TableManager tableManager;
public QueryManager(TableManager tableManager) {
this.tableManager = tableManager;
}
/**
* Query Manager for select queries
*
* @param distinct
* @param columns
* @param where
* @param whereArgs
* @param groupBy
* @param having
* @param orderBy
* @param limit
*/
public void select(boolean distinct, String[] columns, String where, String[] whereArgs, String groupBy, String having, String orderBy, String limit) {
Cursor cursor = this.tableManager.select(distinct, columns, where, whereArgs, groupBy, having, orderBy, limit);
this.onBeforeSelectIterations();
while (cursor.moveToNext()) {
this.onSelectIteration(cursor);
}
this.onAfterSelectIterations();
}
/**
* Query Manager for insert queries
*
* @param values
*/
public void insert(ContentValues values) {
this.onInsertResult(this.tableManager.insert(values));
}
/**
* Query Manager for update queries
*
* @param updatedValues
* @param where
* @param bindArray
*/
public void update(ContentValues updatedValues, String where, String[] bindArray) {
this.onUpdateResult(this.tableManager.update(updatedValues, where, bindArray));
}
/**
* Query Manager for delete queries
*
* @param where
* @param whereArgs
*/
public void delete(String where, String[] whereArgs) {
this.onDeleteResult(this.tableManager.delete(where, whereArgs));
}
/**
* Code executed before the select loop.
* Example:
* //Code executed here
* while(cursor.moveToNext()) {...}
*/
protected abstract void onBeforeSelectIterations();
/**
* Code executed within the select loop.
*
* @param cursor Cursor
* <p/>
* Example:
* while(...)
* {
* //Code executed here
* //Ex: cursor.getString(cursor.getColumnsIndex("field");
* }
*/
protected abstract void onSelectIteration(Cursor cursor);
/**
* Code executed after the select loop.
* Example:
* while(cursor.moveToNext()) {...}
* //Code executed here
*/
protected abstract void onAfterSelectIterations();
/**
* Code executed after the insert query has returned a boolean result
*
* @param insert boolean
*/
protected abstract void onInsertResult(boolean insert);
/**
* Code executed after the update query has returned a boolean result
*
* @param update boolean
*/
protected abstract void onUpdateResult(boolean update);
/**
* Code executed after the delete query has returned a boolean result
*
* @param delete boolean
*/
protected abstract void onDeleteResult(boolean delete);
}
package merlini.mvcpattern.Database;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import merlini.mvcpattern.Base.Console;
/**
* Created by mattia on 16/12/15.
*
* @author Mattia Merlini
*/
public class TableManager {
private String tableName;
private DatabaseHelper databaseHelper;
public TableManager(String tableName, DatabaseHelper databaseHelper) {
this.tableName = tableName;
this.databaseHelper = databaseHelper;
}
/**
* Retrieves records from database
* Example:
* ...select(false, String[] {"id", "name"},
* "id=? AND name=?",
* String[] {"10", "mattia"},
* "name", null, "name, id", "0, 100");
* Explained:
* SELECT id, name FROM tableName WHERE id="10" AND name="mattia" GROUP BY name ORDER BY name, id LIMIT 0, 100;
*/
synchronized public Cursor select(boolean distinct, String[] columns, String where, String[] whereArgs, String groupBy, String having, String orderBy, String limit) {
Cursor cursor = null;
try {
SQLiteDatabase db = this.databaseHelper.getReadableDatabase();
cursor = db.query(distinct, this.tableName, columns, where, whereArgs, groupBy, having, orderBy, limit);
} catch (SQLiteException e) {
Console.log(e);
}
return cursor;
}
/**
* Inserts records to database
* Example:
* ContentValues v = new ContentValues();
* v.put("id", "10");
* v.put("name", "mattia");
* ...insert(v);
* Explained: INSERT INTO tableName (id, name) VALUES ("10", "mattia");
*/
synchronized public boolean insert(ContentValues values) {
try (SQLiteDatabase db = this.databaseHelper.getWritableDatabase()) {
db.insert(this.tableName, null, values);
return true;
} catch (SQLiteException e) {
Console.log(e);
return false;
}
}
/**
* Updates records to database
* Example:
* ContentValues v = new ContentValues();
* v.put("id", "10");
* v.put("name", "mattia");
* ...update(v, "id=? AND name=?", String[] {"11", "merlini"});
* Explained: UPDATE tableName SET id=10, name="mattia" WHERE id="11" AND name="merlini";
*/
synchronized public boolean update(ContentValues updatedValues, String where, String[] bindArray) {
try (SQLiteDatabase db = this.databaseHelper.getWritableDatabase()) {
db.update("Tabella", updatedValues, where, bindArray);
return true;
} catch (SQLiteException e) {
Console.log(e);
return false;
}
}
/**
* Deletes records to database
* Example:
* ...delete("id=? AND name=?", String[] {"10", "mattia"});
* Explained: DELETE FROM tableName WHERE id=10 AND name="mattia";
*/
synchronized public boolean delete(String where, String[] whereArgs) {
try (SQLiteDatabase db = this.databaseHelper.getWritableDatabase()) {
db.delete(this.tableName, where, whereArgs);
return true;
} catch (SQLiteException e) {
Console.log(e);
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment