Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Extend the SQLiteOpenHelper class like so:</p> <pre><code>private static class MyDbHelper extends SQLiteOpenHelper { public MyDbHelper(Context context, String description, CursorFactory factory, int version) { super(context, description, factory, version); } @Override public void onCreate(SQLiteDatabase _db) { _db.execSQL(CREATE_TABLE_1); _db.execSQL(CREATE_TABLE_2); _db.execSQL(CREATE_TABLE_3); ..etc } @Override public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) { // Log the version upgrade. Log.w("MyDbAdapter", "Upgrading from version " + oldVersion + " to " + newVersion + ", which will destroy all old data."); _db.execSQL("DROP TABLE IF EXISTS " + TBL_ONE); _db.execSQL("DROP TABLE IF EXISTS " + TBL_TWO); _db.execSQL("DROP TABLE IF EXISTS " + TBL_THREE); onCreate(_db); } } </code></pre> <p>Create a data adapter class that uses the dbhelper:</p> <pre><code>private SQLiteDatabase db; private MyDbHelper dbHelper; public MyDbAdapter(Context context) { dbHelper = new MyDbHelper(context, DATABASE_NAME, null, DB_VERSION); } public MyDbAdapter open() throws SQLException { try { db = dbHelper.getWritableDatabase(); } catch (SQLiteException ex) { db = dbHelper.getReadableDatabase(); } return this; } public void close() { db.close(); } </code></pre> <p>And then it can be used thus:</p> <pre><code> public class ListsDAO { private Context mContext; private MyDbAdapter db; public ListsDAO(Context context) { mContext = context; db = new MyDbAdapter(mContext); } public List&lt;MyObject&gt; getAllObjects() { List&lt;MyObject&gt; objects = new ArrayList&lt;MyObject&gt;(); db.open(); Cursor cursor = db.getAllObjects(); if (cursor.moveToFirst()) { </code></pre> <p>... etc Once you've got the cursor on a list of rows you can step through them getting the various columns in the table:</p> <p>e.g. description = cursor.getString(descriptionColumn);</p> <p>The static strings such as CREATE_TABLE_1 are basically SQL statements for creating the tables. You may also want a less drastic database upgrade route than simply dropping all the tables and recreating them again.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    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