Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following code is derived from the original Android NotePad tutorial:</p> <pre><code>public class FishDb { private static final String TAG = "FishDb"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; private static final String CREATE_TABLE_FEEDS = "create table feeds (_id integer primary key autoincrement, " + "title text not null, url text not null);"; public static final String DATABASE_NAME = "fish.db3"; public static final String DATABASE_TABLE = "feeds"; private static final int DATABASE_VERSION = 1; private final Context mCtx; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_FEEDS); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS feeds"); onCreate(db); } } public DbAdapter(Context ctx) { this.mCtx = ctx; } public DbAdapter open() throws SQLException { mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this; } public void close() { mDbHelper.close(); } // ... The helper methods } </code></pre> <p>You would instantiate in your Activities onCreate() and possibly open() in onResume(), close() on onPause(): </p> <pre><code>private FishDb mDbHelper; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDbHelper = new FishDb(this); } @Override public void onResume() { super.onResume(); try{ mDbHelper.open(); } catch(Exception e){ Log.e("onResume", e.toString()); } } @Override public void onPause() { super.onPause(); try{ mDbHelper.close(); } catch(Exception e){ Log.e("onPause", e.toString()); } } </code></pre> <p>Hope that helps. </p> <p>EDIT: Use "_id" instead of "feed_id" for primary key.</p>
 

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