Example #1
0
  public void ResetDatabase() {
    RestaurantDbHelper dbHelper;
    if (writableDatabase == null) {
      dbHelper = new RestaurantDbHelper(mContext);

      writableDatabase = dbHelper.getWritableDatabase();
    }
    writableDatabase.execSQL(SQL_DELETE_ENTRIES);
    writableDatabase.execSQL(SQL_CREATE_ENTRIES);
  }
Example #2
0
  public long InsertRestaurant(ETPlace place) {
    RestaurantDbHelper dbHelper;

    if (writableDatabase == null) {
      dbHelper = new RestaurantDbHelper(mContext);

      writableDatabase = dbHelper.getWritableDatabase();
    }

    ContentValues values = new ContentValues();
    values.put(RestaurantEntry.COLUMN_RESTAURANT_UNIQUE, place.getPlaceId());
    values.put(RestaurantEntry.COLUMN_NAME, place.getName());
    values.put(RestaurantEntry.COLUMN_ADDRESS, place.getAddress());
    values.put(RestaurantEntry.COLUMN_LAT, place.getLatitude());
    values.put(RestaurantEntry.COLUMN_LONG, place.getLongitude());
    values.put(RestaurantEntry.COLUMN_FOOD_TYPE, place.getFoodType());
    values.put(RestaurantEntry.COLUMN_RATING, place.getRating());
    values.put(RestaurantEntry.COLUMN_PRICE_LEVEL, place.getPrice().ordinal());

    return writableDatabase.insertOrThrow(RestaurantEntry.TABLE_NAME, null, values);
  }
Example #3
0
  // grab based on food type, rating, price
  // unittest
  public List<ETPlace> GetRestaurants(Integer foodType, Double rating, String price) {

    RestaurantDbHelper dbHelper;
    List<ETPlace> restaurants = new ArrayList<ETPlace>();
    ETPlace place;
    String[] restaurantColumns = {
      RestaurantEntry.COLUMN_RESTAURANT_UNIQUE,
      RestaurantEntry.COLUMN_ADDRESS,
      RestaurantEntry.COLUMN_NAME,
      RestaurantEntry.COLUMN_LAT,
      RestaurantEntry.COLUMN_LONG,
      RestaurantEntry.COLUMN_FOOD_TYPE,
      RestaurantEntry.COLUMN_RATING,
      RestaurantEntry.COLUMN_PRICE_LEVEL
    };
    String selection = "";
    List<String> selectionColumns = new ArrayList<String>();
    int selections = 0;

    if (foodType != null) {
      selections++;
      selection = selection + RestaurantEntry.COLUMN_FOOD_TYPE + "=" + foodType.toString();
    }
    if (rating != null) {
      if (selections > 0) selection = selection + " AND ";
      selections++;
      selection = selection + RestaurantEntry.COLUMN_RATING + "=" + rating.toString();
    }
    if (price != null) {
      if (selections > 0) selection = selection + " AND ";
      selection =
          selection + RestaurantEntry.COLUMN_PRICE_LEVEL + "=" + Price.valueOf(price).ordinal();
    }

    if (writableDatabase == null) {
      dbHelper = new RestaurantDbHelper(mContext);
      writableDatabase = dbHelper.getWritableDatabase();
    }

    // Cursor cursor = writableDatabase.rawQuery("SELECT * FROM " + RestaurantEntry.TABLE_NAME,
    // null);
    Cursor cursor =
        writableDatabase.query(
            RestaurantEntry.TABLE_NAME, // The table to query
            restaurantColumns, // The columns to return
            selection, // The columns for the WHERE clause
            null, // The values for the WHERE clause
            null, // don't group the rows
            null, // don't filter by row groups
            null // The sort order
            );
    Log.d("Testing Info", "row count: " + cursor.getCount());

    if (cursor.getCount() == 0) {
      return null;
    }

    cursor.moveToFirst();

    do {
      place = new ETPlace();

      place.setPlaceId(
          cursor.getString(cursor.getColumnIndexOrThrow(RestaurantEntry.COLUMN_RESTAURANT_UNIQUE)));
      place.setName(cursor.getString(cursor.getColumnIndexOrThrow(RestaurantEntry.COLUMN_NAME)));
      place.setAddress(
          cursor.getString(cursor.getColumnIndexOrThrow(RestaurantEntry.COLUMN_ADDRESS)));
      place.setLatitude(cursor.getDouble(cursor.getColumnIndexOrThrow(RestaurantEntry.COLUMN_LAT)));
      place.setLongitude(
          cursor.getDouble(cursor.getColumnIndexOrThrow(RestaurantEntry.COLUMN_LONG)));
      place.setRating(
          cursor.getDouble(cursor.getColumnIndexOrThrow(RestaurantEntry.COLUMN_RATING)));
      place.setFoodType(
          cursor.getInt(cursor.getColumnIndexOrThrow(RestaurantEntry.COLUMN_FOOD_TYPE)));
      place.setPrice(
          Price.values()[
              cursor.getInt(cursor.getColumnIndexOrThrow(RestaurantEntry.COLUMN_PRICE_LEVEL))]);
      restaurants.add(place);

    } while (cursor.moveToNext());

    return restaurants;
  }