Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way I do is to have a class per table, named after the table with "Table" suffix (e.g. <code>PlayerTable</code> or <code>EventTable</code>).</p> <p>These classes contain all the static variable for the table name and all the field names, and they also contain two static methods:</p> <pre><code>public static void onCreate(SQLiteDatabase database) public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) </code></pre> <p>So that my <code>SQLiteOpenHelper</code> can just call all of them, without having hundreds of static variables with all the fields and create queries. E.g:</p> <pre><code>@Override public void onCreate(SQLiteDatabase database) { PlayerTable.onCreate(database); EventTables.onCreate(database); ..... any other table you have ..... } </code></pre> <p>This class is then injected into all my data access objects (select / update / insert queries). For them I have dedicated classes that contain all my methods, by functionality (e.g. <code>EventHandlingDAO</code> for all the queries that deal with event handling).</p> <p>And finally, theses DAO are injected into the activities that need them, when needed.</p> <p><strong>EDIT:</strong> A few more details about my code:</p> <p>My main objects are the DAO (data access objects), in which I have methods like:</p> <pre><code>// in EventHandlingDAO: public void addEvent(Event event) { SQLiteDatabase database = databaseHelper.getWritableDatabase(); try { database.execSQL("INSERT INTO " + EventTable.EVENT_TABLE_NAME + " (...."); // list of fields and values } finally { database.close(); } } public List&lt;Event&gt; getAllEvents() { final List&lt;Event&gt; result = new ArrayList&lt;Event&gt;(); SQLiteDatabase database = databaseHelper.getReadableDatabase(); try { final Cursor cursor = database.rawQuery("SELECT " + EventTable.KEY_NAME + ", " + EventTable.KEY_DATE_AS_STRING + " FROM " + EventTable.TABLE_NAME, null); cursor.moveToFirst(); // ... rest of the logic, that iterates over the cursor, creates Event objects from the cursor columns and add them to the result list return result; } finally { database.close(); } } </code></pre> <p>So in that DAO, I have my databaseHelper object, which instanciates my class that extends <code>SQLiteOpenHelper</code> with the methods I talked about above.</p> <p>And of course, I have interfaces to all my DAO, so that I can inject a Stub or mocked implementation in my tests (or experiment with different implementations if I want to try another solution based on <code>SharedPreference</code> for example)</p> <p>And the code for my <code>PlayerTable</code> table:</p> <pre><code>public static void onCreate(SQLiteDatabase database) { database.execSQL(TABLE_CREATE); // TABLE_CREATE is my "CREATE TABLE..." query } public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { // A bit blunt, that destroys the data unfortunately, I'll think about doing something more clever later ;) database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(database); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload