1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package com.demo.android.dummynote; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DB { private static final String DATABASE_NAME = "notes.db"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_TABLE = "notes"; private static final String DATABASE_CREATE = "create table notes(" +"_id INTEGER PRIMARY KEY," +"note TEXT," +"created INTEGER," +"modified INTEGER" +");"; private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE); onCreate(db); } } private Context mCtx = null; private DatabaseHelper dbHelper ; private SQLiteDatabase db; /** Constructor */ public DB(Context ctx) { this.mCtx = ctx; } public DB open () throws SQLException { dbHelper = new DatabaseHelper(mCtx); db = dbHelper.getWritableDatabase(); return this; } public void close() { dbHelper.close(); } public static final String KEY_ROWID = "_id"; public static final String KEY_NOTE = "note"; public static final String KEY_CREATED = "created"; public Cursor getAll() { return db.rawQuery("SELECT * FROM notes", null); } } |
Direct link: https://paste.plurk.com/show/589054