Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 5.27 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to properly use databases in Android?
  2. public class PirelliDBAdapter {
  3. private static final String DATABASE_NAME="data";
  4. private static final String DATABASE_TABLE="reminders";
  5. private static final int DATABASE_VERSION=1;
  6.  
  7. public static final String KEY_TITLE="title";
  8. public static final String KEY_BODY="body";
  9.  
  10. public static final String KEY_LOCATION = "location";
  11. public static final String KEY_TYPE = "type";
  12.  
  13. public static final String KEY_DATE_TIME="date_time_value";
  14. public static final String KEY_ROW_ID="_id";
  15.  
  16. private DatabaseHelper mDbHelper;
  17. private SQLiteDatabase db;
  18.  
  19. private static final String DATABASE_CREATE =
  20.         "create table " + DATABASE_TABLE + " ( "+
  21.                 KEY_ROW_ID+ " integer primary key autoincrement, "+
  22.                 KEY_TITLE+ " text not null, "+
  23.                 KEY_BODY+" text not null, "+
  24.                 KEY_DATE_TIME+ " text not null);";
  25.  
  26. private Context mContext;
  27.  
  28. public PirelliDBAdapter(Context c){
  29.     mContext=c;
  30. }
  31.  
  32.  
  33. // database open/close actions
  34. public PirelliDBAdapter open() throws android.database.SQLException{
  35.     mDbHelper=new DatabaseHelper(mContext);
  36.     db=mDbHelper.getWritableDatabase();
  37.     return this;
  38. }
  39.  
  40. public void close(){
  41.     mDbHelper.close();
  42. }
  43.  
  44. // database CRUD operation
  45. public long createReminder(String title, String body, String dateTime){
  46.     ContentValues initialValues = new ContentValues();
  47.     initialValues.put(KEY_TITLE, title);
  48.     initialValues.put(KEY_BODY, body);
  49.     initialValues.put(KEY_DATE_TIME, dateTime);
  50.  
  51.     return db.insert(DATABASE_TABLE, null, initialValues);
  52.  
  53. }
  54.  
  55. public boolean deleteReminder(long rowId){
  56.     return db.delete(DATABASE_TABLE, KEY_ROW_ID+" = "+rowId,null) >0;
  57. }
  58.  
  59. public Cursor fetchAllReminders(){
  60.     return db.query(DATABASE_TABLE,new String [] {KEY_ROW_ID,KEY_TITLE,KEY_BODY,KEY_DATE_TIME}, null, null, null, null, null);
  61. }
  62.  
  63. public Cursor fetchReminder(long rowId) throws SQLException{
  64.     Cursor c = db.query(true, DATABASE_TABLE, new String []{KEY_ROW_ID,KEY_TITLE,KEY_BODY,KEY_DATE_TIME}, KEY_ROW_ID+" = "+rowId, null,null,null,null,null);
  65.  
  66.     if(c!=null){
  67.         c.moveToFirst();
  68.     }
  69.     return c;
  70. }
  71.  
  72. public boolean updateReminder(long rowId, String title, String body, String dateTime){
  73.     ContentValues args = new ContentValues();
  74.     args.put(KEY_TITLE, title);
  75.     args.put(KEY_BODY, body);
  76.     args.put(KEY_DATE_TIME, dateTime);
  77.  
  78.     return db.update(DATABASE_TABLE, args, KEY_ROW_ID+" = "+rowId, null)>0;
  79. }
  80.  
  81. private static class DatabaseHelper extends SQLiteOpenHelper{
  82.     public DatabaseHelper(Context c){
  83.         // create the current database
  84.         super(c,DATABASE_NAME,null,DATABASE_VERSION);
  85.     }
  86.  
  87.     //  overrides
  88.  
  89.     @Override
  90.     public void onCreate(SQLiteDatabase db) {
  91.         // create the database table
  92.         db.execSQL(DATABASE_CREATE);
  93.     }
  94.  
  95.     @Override
  96.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  97.         db.execSQL("drop table " + DATABASE_TABLE);
  98.         db.execSQL(DATABASE_CREATE);
  99.  
  100.     }
  101. }
  102.        
  103. 12-28 22:02:11.806: E/SQLiteDatabase(421):  at  android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
  104.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at pirelli.app.dbadapter.PirelliDBAdapter.open(PirelliDBAdapter.java:44)
  105.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at pirelli.app.AdminSpinnerScreen.onCreate(AdminSpinnerScreen.java:329)
  106.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.app.Activity.performCreate(Activity.java:4397)
  107.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
  108.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
  109.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
  110.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
  111.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
  112.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.os.Handler.dispatchMessage(Handler.java:99)
  113.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.os.Looper.loop(Looper.java:132)
  114.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at android.app.ActivityThread.main(ActivityThread.java:4123)
  115.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at java.lang.reflect.Method.invokeNative(Native Method)
  116.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at java.lang.reflect.Method.invoke(Method.java:491)
  117.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  118.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  119.     12-28 22:02:11.806: E/SQLiteDatabase(421):  at dalvik.system.NativeStart.main(Native Method)
  120.        
  121. public DatabaseHelper(Context c){
  122.         // create the current database
  123.         super(c,DATABASE_NAME,null,DATABASE_VERSION);
  124.         SQLiteDatabase db = this.getWritableDatabase();
  125.         try{//USE THESE WHEN CHANGING DB STRUCTURE
  126.             //db.execSQL("drop table if exists " + DATABASE_TABLE);
  127.             //db.execSQL(DATABASE_CREATE);
  128.         } catch( Exception E ){
  129.             Log.v("ERRRRRRROR", E.getMessage());
  130.         }
  131.     }