Android Open Source - ProjectTimer Timer Db Adapter






From Project

Back to project page ProjectTimer.

License

The source code is released under:

Copyright (c) 2012 Alex Wu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software w...

If you think the Android project ProjectTimer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cooltofu.db;
//from  www  . j a v  a 2 s .  co  m
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TimerDbAdapter {

  // Database fields
  public static final String KEY_ROWID = "_id";
  public static final String KEY_LABEL = "label";
  public static final String KEY_SECONDS = "seconds";
  public static final String KEY_NOTE = "note";
  public static final String KEY_TIMESTAMP = "timestamp";
  public static final String KEY_IS_ON = "is_on";
  private static final String DB_TABLE = "timer";
  
  private Context context;
  private SQLiteDatabase db;
  private TimerDbHelper dbHelper;

  public TimerDbAdapter(Context context) {
    this.context = context;
    
  }

  public TimerDbAdapter open() throws SQLException {
    
    dbHelper = new TimerDbHelper(context);
    db = dbHelper.getWritableDatabase();
  
    return this;
  }

  public void close() {
    dbHelper.close();
  }

  public boolean isOpen() {
    return db.isOpen();
  }
  
/**
   * Create a new timer If the timer is successfully created return the new
   * rowId for that timer, otherwise return a -1 to indicate failure.
   */

  public long createTimer(String label, int seconds, long timestamp, boolean isOn) {
    ContentValues values = createContentValues(label, seconds, timestamp, isOn);

    return db.insert(DB_TABLE, null, values);
  }

  
/**
   * Update the timer
   */

  public boolean updateTimer(long rowId, String label, int seconds, long timestamp, boolean isOn) {
    ContentValues values = createContentValues(label, seconds, timestamp, isOn);

    return db.update(DB_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
  }

  
/**
   * Deletes timer
   */

  public boolean deleteTimer(long rowId) {
    return db.delete(DB_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
  }

  
  
/**
   * Return a Cursor over the list of all timer in the database
   * 
   * @return Cursor over all notes
   */

  public Cursor fetchAllTimers() {
    return db.query(DB_TABLE, new String[] { KEY_ROWID, KEY_LABEL, KEY_SECONDS, KEY_TIMESTAMP, KEY_IS_ON }, null, null, null, null, null);
  }

  
/**
   * Return a Cursor positioned at the defined timer
   */

  public Cursor fetchTimer(long rowId) throws SQLException {
    Cursor mCursor = db.query(true, DB_TABLE, new String[] { KEY_ROWID, KEY_LABEL, KEY_SECONDS, KEY_IS_ON }, KEY_ROWID + "="
        + rowId, null, null, null, null, null);
    if (mCursor != null) {
      mCursor.moveToFirst();
    }
    return mCursor;
  }

  public Cursor getNote(long rowId) throws SQLException {
    Cursor mCursor = db.query(true, DB_TABLE, new String[] { KEY_NOTE }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
    if (mCursor != null) {
      mCursor.moveToFirst();
    }
    return mCursor;
  }
  
  public boolean updateNote(long rowId, String note) {
    ContentValues values = createNoteContentValues(note);
    return db.update(DB_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
  }
  
  private ContentValues createContentValues(String label, int seconds, long timestamp, boolean isOn) {
    ContentValues values = new ContentValues();
    values.put(KEY_LABEL, label);
    values.put(KEY_SECONDS, seconds);
    values.put(KEY_TIMESTAMP, timestamp);
    values.put(KEY_IS_ON, isOn);
    return values;
  }
  
  private ContentValues createNoteContentValues(String note) {
    ContentValues values = new ContentValues();
    values.put(KEY_NOTE, note);
    return values;
  }

}




Java Source Code List

com.cooltofu.Eula.java
com.cooltofu.MoreScreen.java
com.cooltofu.MyTimer.java
com.cooltofu.ProjectTimerActivity.java
com.cooltofu.TimeUtil.java
com.cooltofu.db.TimerDbAdapter.java
com.cooltofu.db.TimerDbHelper.java
com.cooltofu.db.TimerTable.java
com.cooltofu.util.Base64DecoderException.java
com.cooltofu.util.Base64.java
com.cooltofu.util.IabException.java
com.cooltofu.util.IabHelper.java
com.cooltofu.util.IabResult.java
com.cooltofu.util.Inventory.java
com.cooltofu.util.Purchase.java
com.cooltofu.util.Security.java
com.cooltofu.util.SkuDetails.java