Skip to main content

Posts

Showing posts from July, 2014

Enum to build SQLite database in Android

Over the past few days, I have search a "cleanest" way to manage a SQLite database. If the database contains only one table the simplest solution is to put directly into the SQLiteOpenHelper a string query to create/update the table. With several tables, In my opinion, it isn't the smartest solution. So to build the sql query and manage the tables I have used Enum . I have created two Enum types: Columns and Table. The Columns is an Interface which contains an Enum for each table. This Enums are the list of the columns of a table, and have as a value the type of the the variable( INTEGER, REAL,TEXT...), in order to use it to get a piece of query with the getCreateQuery method. public interface Columns { String getCreateQuery(); public enum TableTest1 implements Columns { ID(INTEGER), COL1(TEXT), COL2(REAL) private final String value; private TableTest1(String value) { this.value = value; } @Override public String getCreateQuery() { return th