mirror of
https://github.com/bspeice/itcs4180
synced 2024-12-04 13:18:16 -05:00
Add the Location Database Helper
Not yet tested, but most functionality is done.
This commit is contained in:
parent
e398045a18
commit
d701c2bcb2
@ -0,0 +1,108 @@
|
||||
package edu.uncc.scavenger.database;
|
||||
|
||||
// Design pattern from: http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import edu.uncc.scavenger.rest.RestLocation;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteDatabase.CursorFactory;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
public class LocationDatabaseHelper extends SQLiteOpenHelper {
|
||||
|
||||
private static LocationDatabaseHelper helper;
|
||||
|
||||
private static final String DATABASE_NAME = "locations.db";
|
||||
private static final String TABLE_NAME = "LOCATIONS";
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
|
||||
private static final String KEY_ID = "ID";
|
||||
private static final String KEY_NAME = "NAME";
|
||||
private static final String KEY_RIDDLE = "RIDDLE";
|
||||
private static final String KEY_LOCATION_LONG = "LOCATION_LONG";
|
||||
private static final String KEY_LOCATION_LAT = "LOCATION_LAT";
|
||||
private static final String KEY_KEY = "KEY";
|
||||
|
||||
private LocationDatabaseHelper(Context ctx) {
|
||||
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
public LocationDatabaseHelper getInstance(Context ctx) {
|
||||
if (helper == null) {
|
||||
helper = new LocationDatabaseHelper(ctx.getApplicationContext());
|
||||
}
|
||||
return helper;
|
||||
}
|
||||
|
||||
private static final String CREATE_QUERY =
|
||||
"CREATE TABLE " + TABLE_NAME + "(" +
|
||||
KEY_ID + " int PRIMARY KEY," +
|
||||
KEY_NAME + " VARCHAR(255)," +
|
||||
KEY_RIDDLE + " VARCHAR(1024)," +
|
||||
KEY_LOCATION_LONG + " REAL," +
|
||||
KEY_LOCATION_LAT + " REAL" +
|
||||
KEY_KEY + " VARCHAR(255)" +
|
||||
");";
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL(CREATE_QUERY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
return;
|
||||
}
|
||||
|
||||
public RestLocation locationFromCursor(Cursor c) {
|
||||
RestLocation location = null;
|
||||
if (c != null) {
|
||||
location = new RestLocation();
|
||||
location.setId(c.getInt(0));
|
||||
location.setName(c.getString(1));
|
||||
location.setRiddle(c.getString(2));
|
||||
location.setLocationLong(c.getDouble(3));
|
||||
location.setLocationLat(c.getDouble(4));
|
||||
location.setKey(c.getString(5));
|
||||
}
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
public ArrayList<RestLocation> fetchAll() {
|
||||
ArrayList<RestLocation> results = new ArrayList<RestLocation>();
|
||||
SQLiteDatabase db = helper.getReadableDatabase();
|
||||
Cursor c = db.query(TABLE_NAME, new String[]{KEY_ID, KEY_NAME, KEY_RIDDLE, KEY_LOCATION_LONG,
|
||||
KEY_LOCATION_LAT, KEY_KEY},
|
||||
null, null, null, null, null);
|
||||
|
||||
if (c != null && c.getCount() > 0) {
|
||||
c.moveToFirst();
|
||||
do {
|
||||
RestLocation loc = locationFromCursor(c);
|
||||
if (loc != null) {
|
||||
results.add(loc);
|
||||
}
|
||||
} while (c.moveToNext());
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public void persist(RestLocation loc) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(KEY_ID, loc.getId());
|
||||
values.put(KEY_NAME, loc.getName());
|
||||
values.put(KEY_RIDDLE, loc.getRiddle());
|
||||
values.put(KEY_LOCATION_LONG, loc.getLocationLong());
|
||||
values.put(KEY_LOCATION_LAT, loc.getLocationLat());
|
||||
values.put(KEY_KEY, loc.getKey());
|
||||
helper.getWritableDatabase().insert(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -9,6 +9,7 @@ public class RestLocation {
|
||||
private String riddle;
|
||||
private double locationLong;
|
||||
private double locationLat;
|
||||
private String key;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
@ -40,6 +41,12 @@ public class RestLocation {
|
||||
public void setLocationLat(double locationLat) {
|
||||
this.locationLat = locationLat;
|
||||
}
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public Location getLocation() {
|
||||
android.location.Location mLocation = new android.location.Location("NinerFinderServer");
|
||||
|
Loading…
Reference in New Issue
Block a user