Using java Syntax Highlighting
- public class Settings implements ActiveRecord {
- // writable members
- private int checkInterval;
- private boolean checkOnMonday;
- private boolean checkOnTuesday;
- private boolean checkOnWednesday;
- private boolean checkOnThursday;
- private boolean checkOnFriday;
- private boolean checkOnSaturday;
- private boolean checkOnSunday;
- private Date checkStartTime;
- private Date checkEndTime;
- private int accuracy;
- private String type;
- private int turnOffUnderPercentage;
- private SQLiteDatabase sqliteDatabase;
- // constants
- public static final String COL_TYPE = "type";
- public static final String COL_CHECK_INTERVAL = "check_interval";
- public static final String COL_ACCURACY = "accuracy";
- public static final String COL_CHECK_START_TIME = "check_start_time";
- public static final String COL_CHECK_END_TIME = "check_end_time";
- public static final String COL_CHECK_MONDAY = "check_monday";
- public static final String COL_CHECK_TUESDAY = "check_tuesday";
- public static final String COL_CHECK_WEDNESDAY = "check_wednesday";
- public static final String COL_CHECK_THURSDAY = "check_thursday";
- public static final String COL_CHECK_FRIDAY = "check_friday";
- public static final String COL_CHECK_SATURDAY = "check_saturday";
- public static final String COL_CHECK_SUNDAY = "check_sunday";
- public static final String COL_TURN_OFF_UNDER_PERCENTAGE = "turn_off_under_percentage";
- public static final String SQL_TABLE_NAME = "settings";
- public static final String PRESET_TYPE_HIGH_BATTERY_LIFE = "High Battery Life";
- public static final String PRESET_TYPE_HIGH_PERFORMANCE = "High Performance";
- public static final String PRESET_TYPE_CURRENT = "Current";
- public static final String SQL_CREATE_TABLE = "CREATE TABLE "
- + SQL_TABLE_NAME
- + " "
- + "(type text primary key, check_interval integer not null, "
- + "accuracy integer not null, check_start_time text not null, "
- + "check_end_time text not null, check_monday integer not null, "
- + "check_tuesday integer not null, check_wednesday integer not null, "
- + "check_thursday integer not null, check_friday integer not null, "
- + "check_saturday integer not null, check_sunday integer not null, "
- + "turn_off_under_percentage integer not null); ";
- public static final String SQL_INSERT_PRESET_HBL = "INSERT INTO \""
- + SQL_TABLE_NAME + "\" VALUES( 'High Battery Life',"
- + " 30, 50, '06:00:00', '18:00:00', 1, 1, 1, 1, 1, 0, 0, 30);";
- public static final String SQL_INSERT_PRESET_HP = "INSERT INTO \""
- + SQL_TABLE_NAME + "\" VALUES( 'High Performance',"
- + " 5, 10, '00:00:00', '23:00:00', 1, 1, 1, 1, 1, 1, 1, 10);";
- public static final String SQL_INSERT_PRESET_C = "INSERT INTO \""
- + SQL_TABLE_NAME + "\" VALUES( 'Current',"
- + " 15, 25, '04:00:00', '21:00:00', 1, 1, 1, 1, 1, 0, 0, 20);";
- // accessors
- public int getCheckInterval() {
- return checkInterval;
- }
- public void setCheckInterval(int checkInterval) {
- this.checkInterval = checkInterval;
- }
- public int getAccuracy() {
- return accuracy;
- }
- public void setAccuracy(int accuracy) {
- this.accuracy = accuracy;
- }
- public Date getCheckStartTime() {
- return checkStartTime;
- }
- public void setCheckStartTime(Date checkStartTime) {
- this.checkStartTime = checkStartTime;
- }
- public Date getCheckEndTime() {
- return checkEndTime;
- }
- public void setCheckEndTime(Date checkEndTime) {
- this.checkEndTime = checkEndTime;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public int getTurnOffUnderPercentage() {
- return turnOffUnderPercentage;
- }
- public void setTurnOffUnderPercentage(int turnOffUnderPercentage) {
- this.turnOffUnderPercentage = turnOffUnderPercentage;
- }
- public boolean getCheckOnMonday() {
- return checkOnMonday;
- }
- public void setCheckOnMonday(boolean checkOnMonday) {
- this.checkOnMonday = checkOnMonday;
- }
- public boolean getCheckOnTuesday() {
- return checkOnTuesday;
- }
- public void setCheckOnTuesday(boolean checkOnTuesday) {
- this.checkOnTuesday = checkOnTuesday;
- }
- public boolean getCheckOnWednesday() {
- return checkOnWednesday;
- }
- public void setCheckOnWednesday(boolean checkOnWednesday) {
- this.checkOnWednesday = checkOnWednesday;
- }
- public boolean getCheckOnThursday() {
- return checkOnThursday;
- }
- public void setCheckOnThursday(boolean checkOnThursday) {
- this.checkOnThursday = checkOnThursday;
- }
- public boolean getCheckOnFriday() {
- return checkOnFriday;
- }
- public void setCheckOnFriday(boolean checkOnFriday) {
- this.checkOnFriday = checkOnFriday;
- }
- public boolean getCheckOnSaturday() {
- return checkOnSaturday;
- }
- public void setCheckOnSaturday(boolean checkOnSaturday) {
- this.checkOnSaturday = checkOnSaturday;
- }
- public boolean getCheckOnSunday() {
- return checkOnSunday;
- }
- public void setCheckOnSunday(boolean checkOnSunday) {
- this.checkOnSunday = checkOnSunday;
- }
- // ActiveRecord Implementation
- // loads an instance of goal based on the provided id
- // when called from an activity pass this
- public void load(Activity activity) {
- // here we build and execute our query based on constant members
- Cursor cursor = sqliteDatabase.query(SQL_TABLE_NAME, null, COL_TYPE
- + "='" + type + "'", null, null, null, null);
- // we use the cursor returned from our query to populate this
- // object's writable members
- if (cursor != null && cursor.moveToFirst()) {
- // this allows the provided activity to manage the cursors life
- // cycle
- activity.startManagingCursor(cursor);
- try {
- SimpleDateFormat formater = new SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss");
- setType(cursor.getString(cursor.getColumnIndex(COL_TYPE)));
- setCheckInterval(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_INTERVAL)));
- setAccuracy(cursor.getInt(cursor.getColumnIndex(COL_ACCURACY)));
- setCheckStartTime(formater.parse("0000-00-00 "
- + cursor.getString(cursor
- .getColumnIndex(COL_CHECK_START_TIME))));
- setCheckEndTime(formater.parse("0000-00-00 "
- + cursor.getString(cursor
- .getColumnIndex(COL_CHECK_END_TIME))));
- setTurnOffUnderPercentage(cursor.getInt(cursor
- .getColumnIndex(COL_TURN_OFF_UNDER_PERCENTAGE)));
- setCheckOnMonday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_MONDAY)) != 0);
- setCheckOnTuesday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_TUESDAY)) != 0);
- setCheckOnWednesday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_WEDNESDAY)) != 0);
- setCheckOnThursday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_THURSDAY)) != 0);
- setCheckOnFriday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_FRIDAY)) != 0);
- setCheckOnSaturday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_SATURDAY)) != 0);
- setCheckOnSunday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_SUNDAY)) != 0);
- } catch (Exception e) {
- Log.e("Settings.load(): ", e.getMessage());
- }
- }
- }
- // loads an instance of goal based on the provided id
- // when called from an activity pass this
- public void load() {
- // here we build and execute our query based on constant members
- Cursor cursor = sqliteDatabase.query(SQL_TABLE_NAME, null, COL_TYPE
- + "='" + type + "'", null, null, null, null);
- // we use the cursor returned from our query to populate this
- // object's writable members
- if (cursor != null && cursor.moveToFirst()) {
- try {
- SimpleDateFormat formater = new SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss");
- setType(cursor.getString(cursor.getColumnIndex(COL_TYPE)));
- setCheckInterval(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_INTERVAL)));
- setAccuracy(cursor.getInt(cursor.getColumnIndex(COL_ACCURACY)));
- setCheckStartTime(formater.parse("0000-00-00 "
- + cursor.getString(cursor
- .getColumnIndex(COL_CHECK_START_TIME))));
- setCheckEndTime(formater.parse("0000-00-00 "
- + cursor.getString(cursor
- .getColumnIndex(COL_CHECK_END_TIME))));
- setTurnOffUnderPercentage(cursor.getInt(cursor
- .getColumnIndex(COL_TURN_OFF_UNDER_PERCENTAGE)));
- setCheckOnMonday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_MONDAY)) != 0);
- setCheckOnTuesday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_TUESDAY)) != 0);
- setCheckOnWednesday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_WEDNESDAY)) != 0);
- setCheckOnThursday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_THURSDAY)) != 0);
- setCheckOnFriday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_FRIDAY)) != 0);
- setCheckOnSaturday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_SATURDAY)) != 0);
- setCheckOnSunday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_SUNDAY)) != 0);
- } catch (Exception e) {
- Log.e("Settings.load(): ", e.getMessage());
- }
- }
- }
- // this simply returns a cursor with all setting objects
- // this can be changed to return a list, however certain
- // activity classes have methods meant to interact with a cursor object
- public Cursor retrieveAll() {
- return sqliteDatabase.query(SQL_TABLE_NAME, new String[] { COL_TYPE,
- COL_CHECK_INTERVAL, COL_ACCURACY, COL_CHECK_START_TIME,
- COL_CHECK_END_TIME, COL_TURN_OFF_UNDER_PERCENTAGE,
- COL_CHECK_MONDAY, COL_CHECK_TUESDAY, COL_CHECK_WEDNESDAY,
- COL_CHECK_THURSDAY, COL_CHECK_FRIDAY, COL_CHECK_SATURDAY,
- COL_CHECK_SUNDAY }, null, null, null, null, null);
- }
- //returns a list of all settings
- public List<Settings> retrieveList()
- {
- List<Settings> settings = new ArrayList<Settings>();
- Cursor cursor = sqliteDatabase.query(SQL_TABLE_NAME, new String[] { COL_TYPE,
- COL_CHECK_INTERVAL, COL_ACCURACY, COL_CHECK_START_TIME,
- COL_CHECK_END_TIME, COL_TURN_OFF_UNDER_PERCENTAGE,
- COL_CHECK_MONDAY, COL_CHECK_TUESDAY, COL_CHECK_WEDNESDAY,
- COL_CHECK_THURSDAY, COL_CHECK_FRIDAY, COL_CHECK_SATURDAY,
- COL_CHECK_SUNDAY }, null, null, null, null, null);
- if (cursor != null && cursor.moveToFirst()) {
- do{
- try {
- Settings s = new Settings();
- SimpleDateFormat formater = new SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss");
- s.setType(cursor.getString(cursor.getColumnIndex(COL_TYPE)));
- s.setCheckInterval(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_INTERVAL)));
- s.setAccuracy(cursor.getInt(cursor.getColumnIndex(COL_ACCURACY)));
- s.setCheckStartTime(formater.parse("0000-00-00 "
- + cursor.getString(cursor
- .getColumnIndex(COL_CHECK_START_TIME))));
- s.setCheckEndTime(formater.parse("0000-00-00 "
- + cursor.getString(cursor
- .getColumnIndex(COL_CHECK_END_TIME))));
- s.setTurnOffUnderPercentage(cursor.getInt(cursor
- .getColumnIndex(COL_TURN_OFF_UNDER_PERCENTAGE)));
- s.setCheckOnMonday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_MONDAY)) != 0);
- s.setCheckOnTuesday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_TUESDAY)) != 0);
- s.setCheckOnWednesday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_WEDNESDAY)) != 0);
- s.setCheckOnThursday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_THURSDAY)) != 0);
- s.setCheckOnFriday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_FRIDAY)) != 0);
- s.setCheckOnSaturday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_SATURDAY)) != 0);
- s.setCheckOnSunday(cursor.getInt(cursor
- .getColumnIndex(COL_CHECK_SUNDAY)) != 0);
- s.setSQLiteDatabase(sqliteDatabase);
- settings.add(s);
- } catch (Exception e) {
- Log.e("Settings.load(): ", e.getMessage());
- }
- }while(cursor.moveToNext());
- }
- return settings;
- }
- // this handles updating of settings, currently this does not support
- // inserting
- // new settings, we can simply update the 3 pre-existing settings
- public long save() {
- ContentValues values = new ContentValues();
- SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- values.put(COL_CHECK_INTERVAL, getCheckInterval());
- values.put(COL_ACCURACY, getAccuracy());
- values.put(COL_CHECK_START_TIME, formater.format(getCheckStartTime()));
- values.put(COL_CHECK_END_TIME, formater.format(getCheckEndTime()));
- values.put(COL_CHECK_MONDAY, getCheckOnMonday() ? 1 : 0);
- values.put(COL_CHECK_TUESDAY, getCheckOnTuesday() ? 1 : 0);
- values.put(COL_CHECK_WEDNESDAY, getCheckOnWednesday() ? 1 : 0);
- values.put(COL_CHECK_THURSDAY, getCheckOnThursday() ? 1 : 0);
- values.put(COL_CHECK_FRIDAY, getCheckOnFriday() ? 1 : 0);
- values.put(COL_CHECK_SATURDAY, getCheckOnSaturday() ? 1 : 0);
- values.put(COL_CHECK_SUNDAY, getCheckOnSunday() ? 1 : 0);
- values.put(COL_TURN_OFF_UNDER_PERCENTAGE, getTurnOffUnderPercentage());
- sqliteDatabase.execSQL("UPDATE " + SQL_TABLE_NAME +" SET " +
- COL_CHECK_INTERVAL +" = " +getCheckInterval() + " , " +
- COL_ACCURACY +" = " +getAccuracy() + " , " +
- COL_CHECK_START_TIME +" = " +"'"+ formater.format(getCheckStartTime())+"'" + " , " +
- COL_CHECK_END_TIME+" = " +"'"+formater.format(getCheckEndTime())+"'" + " , " +
- COL_CHECK_MONDAY +" = " +(getCheckOnMonday() ? 1 : 0) + " , " +
- COL_CHECK_TUESDAY +" = " +(getCheckOnTuesday() ? 1 : 0) + " , " +
- COL_CHECK_WEDNESDAY +" = " +(getCheckOnWednesday() ? 1 : 0) + " , " +
- COL_CHECK_THURSDAY +" = " +(getCheckOnThursday() ? 1 : 0) + " , " +
- COL_CHECK_FRIDAY +" = " +(getCheckOnFriday() ? 1 : 0) + " , " +
- COL_CHECK_SATURDAY +" = " +(getCheckOnSaturday() ? 1 : 0) + " , " +
- COL_CHECK_SUNDAY +" = " +(getCheckOnSunday() ? 1 : 0) + " , " +
- COL_TURN_OFF_UNDER_PERCENTAGE +" = " +getTurnOffUnderPercentage() +
- " WHERE " +COL_TYPE + " = "+ "'"+getType()+"'");
- return 0;//sqliteDatabase.update(SQL_TABLE_NAME, values, COL_TYPE + " = "
- //+ "'"+getType()+"'", null);
- }
- // we never want to delete presets or custom settings
- public boolean delete() {
- return false;
- }
- // simple setter for this objects SQLiteDatabase member
- public void setSQLiteDatabase(SQLiteDatabase sqliteDatabase) {
- this.sqliteDatabase = sqliteDatabase;
- }
- }
Parsed in 0.087 seconds, using GeSHi 1.0.8.4
I used both raw sql and the provided update method and both are acting the same. Could this have to do with the primary key of the table being a string?
any help would be greatly appreciated.

