Advertisement
Ankhwatcher

FixOpenHelper

Mar 29th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. package ie.appz.shortestwalkingroute.sqlite;
  2.  
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.location.Location;
  8. import android.util.Log;
  9.  
  10. public class FixOpenHelper extends SQLiteOpenHelper {
  11.  
  12.     private static final String DATABASE_NAME = "fixtable.db";
  13.     private static final int DATABASE_VERSION = 3;
  14.     public static final String TABLE_NAME = "fix_table";
  15.     public static final String COLUMN_ID = "_id";
  16.     public static final String ROUTE_NUMBER = "route_number";
  17.     public static final String LATITUDE = "latitude";
  18.     public static final String LONGITUDE = "longitude";
  19.     public static final String ACCURACY = "accuracy";
  20.     public static final String SPEED = "speed";
  21.     public static final String SOURCE = "source";
  22.     public static final String TIME = "time";
  23.     public static final String TARGET = "target";
  24.  
  25.     // Database creation SQL statement
  26.     private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + COLUMN_ID
  27.             + " integer primary key autoincrement, " + ROUTE_NUMBER + " integer, " + TARGET + " integer," + LATITUDE
  28.             + " real," + LONGITUDE + " real," + ACCURACY + " real," + SPEED + " real," + TIME + " integer, " + SOURCE
  29.             + " text not null" + ");";
  30.  
  31.     public FixOpenHelper(Context context) {
  32.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  33.     }
  34.  
  35.     @Override
  36.     public void onCreate(SQLiteDatabase db) {
  37.         db.execSQL(DATABASE_CREATE);
  38.     }
  39.  
  40.     @Override
  41.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  42.         Log.w(FixOpenHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion
  43.                 + ", which will destroy all old data");
  44.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  45.         onCreate(db);
  46.     }
  47.  
  48.     public void addFix(int routeNo, Location location) {
  49.  
  50.         SQLiteDatabase db = getWritableDatabase();
  51.         db.execSQL("insert into " + TABLE_NAME + "(" + ROUTE_NUMBER + ", " + LATITUDE + ", " + LONGITUDE + ", "
  52.                 + ACCURACY + ", " + SPEED + ", " + SOURCE + ", " + TIME + ") " + "values(" + routeNo + ", "
  53.                 + location.getLatitude() + ", " + location.getLongitude() + ", " + location.getAccuracy() + ", "
  54.                 + location.getSpeed() + "," + " '" + location.getProvider() + "', " + location.getTime() + ");");
  55.  
  56.     }
  57.  
  58.     public int highestRoute() {
  59.         SQLiteDatabase db = getReadableDatabase();
  60.         int highRoute = 0;
  61.         try {
  62.  
  63.             Cursor results = db.rawQuery("SELECT MAX(" + ROUTE_NUMBER + ") FROM " + TABLE_NAME, null);
  64.             if (results.moveToFirst()) {
  65.                 highRoute = results.getInt(0);
  66.             }
  67.             results.close();
  68.         } catch (Exception e) {
  69.             Log.e(FixOpenHelper.class.getName(), "Unable to get highestRoute.", e);
  70.         }
  71.  
  72.         return highRoute;
  73.  
  74.     }
  75.  
  76.     public long totalRouteTime(int routeNo) {
  77.         long diffTime = 0;
  78.         SQLiteDatabase db = getReadableDatabase();
  79.         try {
  80.             Cursor results = db.rawQuery("SELECT MAX(" + TIME + ") AS \"MAXTIME\", MIN(" + TIME
  81.                     + ") AS \"MINTIME\" FROM " + TABLE_NAME + " WHERE " + ROUTE_NUMBER + " == " + routeNo, null);
  82.             if (results.moveToFirst()) {
  83.                 diffTime = results.getLong(0) - results.getLong(1);
  84.             }
  85.             results.close();
  86.         } catch (Exception e) {
  87.             Log.e(FixOpenHelper.class.getName(), "Unable to get diffTime for Route " + routeNo + ".", e);
  88.         }
  89.         return diffTime;
  90.     }
  91.  
  92.     public float minimumAccuracy(int routeNo) {
  93.         float minAcc = 0;
  94.         SQLiteDatabase db = getReadableDatabase();
  95.         try {
  96.             Cursor results = db.rawQuery("SELECT MIN(" + ACCURACY + ") FROM " + TABLE_NAME, null);
  97.             if (results.moveToFirst()) {
  98.                 minAcc = results.getFloat(0);
  99.             }
  100.             results.close();
  101.         } catch (Exception e) {
  102.             Log.e(FixOpenHelper.class.getName(), "Unable to get minimum Accuracy for Route " + routeNo + ".", e);
  103.         }
  104.         return minAcc;
  105.     }
  106.  
  107.     public Cursor routeFixes(int routeNo) {
  108.         SQLiteDatabase db = getReadableDatabase();
  109.  
  110.         return db.query(TABLE_NAME, new String[] { LATITUDE, LONGITUDE, ACCURACY, TIME }, ROUTE_NUMBER + " == "
  111.                 + routeNo, null, null, null, COLUMN_ID);
  112.  
  113.     }
  114.  
  115.     public void rejectRoute(int routeNo) {
  116.         SQLiteDatabase db = getReadableDatabase();
  117.         db.delete(TABLE_NAME, ROUTE_NUMBER + "=" + routeNo, null);
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement