Android Open Source - robotronic Database Handler






From Project

Back to project page robotronic.

License

The source code is released under:

Copyright (C) 2011 by Drew Schrauf 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 So...

If you think the Android project robotronic 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.drewschrauf.robotronic.database;
//from w ww . j a  v  a  2  s  .c om
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DatabaseHandler {

  DatabaseHelper helper;

  public DatabaseHandler(Context context) {
    helper = new DatabaseHelper(context);
  }

  public void addData(String url, String data) {
    try {
      SQLiteDatabase db = helper.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put(DatabaseHelper.COLUMN_URL, url);
      values.put(DatabaseHelper.COLUMN_DATA, data);
      values.put(DatabaseHelper.COLUMN_FETCHED_DATE,
          System.currentTimeMillis());
      db.replace(DatabaseHelper.TABLE_NAME, null, values);
      db.close();
    } catch (Exception e) {
      // database was closed before it could be written to, just skip it
      // this time
      Log.e("DatabaseHandler",
          "Database could not be written to, skipping cache");
    }
  }

  /**
   * Fetch the cached data for the given URL
   * 
   * @param url
   *            The URL to be looked up in the cache
   * @return The cached data from the URL or null if there is none available
   */
  public String getData(String url) {
    try {
      SQLiteDatabase db = helper.getReadableDatabase();
      String[] columns = { DatabaseHelper.COLUMN_DATA };
      Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, columns,
          DatabaseHelper.COLUMN_URL + " = '" + url + "'", null, null,
          null, "1");
      String data = null;
      while (cursor.moveToNext()) {
        data = cursor.getString(0);
      }
      cursor.close();
      db.close();
      return data;
    } catch (Exception e) {
      // database was closed before it could be read, just skip it this
      // time
      Log.e("DatabaseHandler",
          "Database could not be read, skipping cache");
      return null;
    }

  }
  
  /**
   * Delete all the cached data
   *    
   */
  public void deleteAllData() {
    try {
      SQLiteDatabase db = helper.getReadableDatabase();
      db.delete(DatabaseHelper.TABLE_NAME, null, null);
      db.close();
    } catch (Exception e) {
      // database was closed before it could be written to, just skip it
      // this time
      Log.e("DatabaseHandler",
          "Database could not be written to, skipping deleteAll");
    }

  }

}




Java Source Code List

com.drewschrauf.example.robotronic.ExampleHome.java
com.drewschrauf.example.robotronic.ExampleListItem.java
com.drewschrauf.example.robotronic.ExampleList.java
com.drewschrauf.example.robotronic.ExampleSimple.java
com.drewschrauf.robotronic.activities.RobotronicActivity.java
com.drewschrauf.robotronic.activities.RobotronicListActivity.java
com.drewschrauf.robotronic.database.DatabaseHandler.java
com.drewschrauf.robotronic.database.DatabaseHelper.java
com.drewschrauf.robotronic.threads.BinaryFetchThread.java
com.drewschrauf.robotronic.threads.CacheCleaner.java
com.drewschrauf.robotronic.threads.DataFetchThread.java
com.drewschrauf.robotronic.threads.ParsingException.java
com.drewschrauf.robotronic.threads.RobotronicProperties.java
com.drewschrauf.robotronic.threads.RobotronicThread.java
com.drewschrauf.robotronic.threads.RobotronicUtilities.java
com.drewschrauf.robotronic.threads.ThreadHandler.java