Android Open Source - FlyingDB Database Adapter






From Project

Back to project page FlyingDB.

License

The source code is released under:

MIT License

If you think the Android project FlyingDB 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

//**********************************************************
//************** AUTOGENERATED with FLYINGDB ***************
//**********************************************************
// w w  w  . j a  v a 2  s  .c  om
package com.flyingboba.flyingdb;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DatabaseAdapter {

  private DatabaseHelper databaseHelper;
  private Context myContext;
  
  public DatabaseAdapter(Context context) {
    myContext = context;
  }

  public void open() {
    Log.i("DatabaseAdapter", "Opening database connection");

    databaseHelper = new DatabaseHelper(myContext);
    
    // Call createDatabase only if planning to copy an existing database from the .apk file 
    // over to storage space on the phone
    try {
      databaseHelper.createDatabase();
    } catch(IOException ioe) {
      Log.i("IO Exception, could not create database", ioe.getStackTrace().toString());
    }
  }
  
  public void close() {
    Log.i("DatabaseAdapter", "Closing database connection");
    databaseHelper.close();
  }
  
  

  // -----------------------------------------------------------------------------
  // Person Query Methods
  // -----------------------------------------------------------------------------

  public List<PrefixPerson> getPersonList(String fieldName, String fieldValue, String orderBy)   {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    Cursor result = null;
    String query = "SELECT id, firstName, lastName, email, phone FROM Person";
    if(fieldName!=null && fieldName.length()>0) {
      query += " WHERE " + fieldName + " = ?";
    }

    if(orderBy!=null && orderBy.length()>0) {
      query += " ORDER BY " + orderBy;
    }

    if(fieldName!=null && fieldName.length()>0) {
      result = database.rawQuery(query, new String[] { fieldValue });
    } else {
      result = database.rawQuery(query, null);
    }
    
    List<PrefixPerson> returnList = new ArrayList<PrefixPerson>();
    int rowCount = result.getCount();
    if (rowCount > 0) {
      result.moveToFirst();
      while (!result.isAfterLast()) {
        returnList.add(new PrefixPerson(result.getString(0), result.getString(1), result.getString(2), result.getString(3), result.getString(4)));
        result.moveToNext();
      }
    }
    result.close();
    return returnList;
  }

  public long addPerson(String aFirstName, String aLastName, String aEmail, String aPhone) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("firstName", aFirstName);
    values.put("lastName", aLastName);
    values.put("email", aEmail);
    values.put("phone", aPhone);
    
    long result = database.insert("Person", null, values);
    return result;
  }

  public long addPerson(PrefixPerson pojo) {
    return addPerson(pojo.getFirstName(), pojo.getLastName(), pojo.getEmail(), pojo.getPhone());
  }

  public long updatePerson(String aFirstName, String aLastName, String aEmail, String aPhone, String fieldName, String fieldValue) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("firstName", aFirstName);
    values.put("lastName", aLastName);
    values.put("email", aEmail);
    values.put("phone", aPhone);
    
    long result = database.update("Person", values, fieldName + " = ?", 
      new String[] { fieldValue } );
    return result;
  }

  public long updatePerson(PrefixPerson pojo, String fieldName, String fieldValue) {
    return updatePerson(pojo.getFirstName(), pojo.getLastName(), pojo.getEmail(), pojo.getPhone(), fieldName, fieldValue);
  }

  public int removePerson(String fieldName, String fieldValue) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    int result = database.delete("Person", fieldName + " = ?",
        new String[] { fieldValue });
    return result;
  }

  // -----------------------------------------------------------------------------
  // Event Query Methods
  // -----------------------------------------------------------------------------

  public List<PrefixEvent> getEventList(String fieldName, String fieldValue, String orderBy)   {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    Cursor result = null;
    String query = "SELECT id, date, locationId, title FROM Event";
    if(fieldName!=null && fieldName.length()>0) {
      query += " WHERE " + fieldName + " = ?";
    }

    if(orderBy!=null && orderBy.length()>0) {
      query += " ORDER BY " + orderBy;
    }

    if(fieldName!=null && fieldName.length()>0) {
      result = database.rawQuery(query, new String[] { fieldValue });
    } else {
      result = database.rawQuery(query, null);
    }
    
    List<PrefixEvent> returnList = new ArrayList<PrefixEvent>();
    int rowCount = result.getCount();
    if (rowCount > 0) {
      result.moveToFirst();
      while (!result.isAfterLast()) {
        returnList.add(new PrefixEvent(result.getString(0), result.getString(1), result.getString(2), result.getString(3)));
        result.moveToNext();
      }
    }
    result.close();
    return returnList;
  }

  public long addEvent(String aDate, String aLocationId, String aTitle) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("date", aDate);
    values.put("locationId", aLocationId);
    values.put("title", aTitle);
    
    long result = database.insert("Event", null, values);
    return result;
  }

  public long addEvent(PrefixEvent pojo) {
    return addEvent(pojo.getDate(), pojo.getLocationId(), pojo.getTitle());
  }

  public long updateEvent(String aDate, String aLocationId, String aTitle, String fieldName, String fieldValue) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("date", aDate);
    values.put("locationId", aLocationId);
    values.put("title", aTitle);
    
    long result = database.update("Event", values, fieldName + " = ?", 
      new String[] { fieldValue } );
    return result;
  }

  public long updateEvent(PrefixEvent pojo, String fieldName, String fieldValue) {
    return updateEvent(pojo.getDate(), pojo.getLocationId(), pojo.getTitle(), fieldName, fieldValue);
  }

  public int removeEvent(String fieldName, String fieldValue) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    int result = database.delete("Event", fieldName + " = ?",
        new String[] { fieldValue });
    return result;
  }

  // -----------------------------------------------------------------------------
  // Location Query Methods
  // -----------------------------------------------------------------------------

  public List<PrefixLocation> getLocationList(String fieldName, String fieldValue, String orderBy)   {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    Cursor result = null;
    String query = "SELECT id, streetAddress, city, state, zipcode, latitude, longitude, title FROM Location";
    if(fieldName!=null && fieldName.length()>0) {
      query += " WHERE " + fieldName + " = ?";
    }

    if(orderBy!=null && orderBy.length()>0) {
      query += " ORDER BY " + orderBy;
    }

    if(fieldName!=null && fieldName.length()>0) {
      result = database.rawQuery(query, new String[] { fieldValue });
    } else {
      result = database.rawQuery(query, null);
    }
    
    List<PrefixLocation> returnList = new ArrayList<PrefixLocation>();
    int rowCount = result.getCount();
    if (rowCount > 0) {
      result.moveToFirst();
      while (!result.isAfterLast()) {
        returnList.add(new PrefixLocation(result.getString(0), result.getString(1), result.getString(2), result.getString(3), result.getString(4), result.getString(5), result.getString(6), result.getString(7)));
        result.moveToNext();
      }
    }
    result.close();
    return returnList;
  }

  public long addLocation(String aStreetAddress, String aCity, String aState, String aZipcode, String aLatitude, String aLongitude, String aTitle) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("streetAddress", aStreetAddress);
    values.put("city", aCity);
    values.put("state", aState);
    values.put("zipcode", aZipcode);
    values.put("latitude", aLatitude);
    values.put("longitude", aLongitude);
    values.put("title", aTitle);
    
    long result = database.insert("Location", null, values);
    return result;
  }

  public long addLocation(PrefixLocation pojo) {
    return addLocation(pojo.getStreetAddress(), pojo.getCity(), pojo.getState(), pojo.getZipcode(), pojo.getLatitude(), pojo.getLongitude(), pojo.getTitle());
  }

  public long updateLocation(String aStreetAddress, String aCity, String aState, String aZipcode, String aLatitude, String aLongitude, String aTitle, String fieldName, String fieldValue) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("streetAddress", aStreetAddress);
    values.put("city", aCity);
    values.put("state", aState);
    values.put("zipcode", aZipcode);
    values.put("latitude", aLatitude);
    values.put("longitude", aLongitude);
    values.put("title", aTitle);
    
    long result = database.update("Location", values, fieldName + " = ?", 
      new String[] { fieldValue } );
    return result;
  }

  public long updateLocation(PrefixLocation pojo, String fieldName, String fieldValue) {
    return updateLocation(pojo.getStreetAddress(), pojo.getCity(), pojo.getState(), pojo.getZipcode(), pojo.getLatitude(), pojo.getLongitude(), pojo.getTitle(), fieldName, fieldValue);
  }

  public int removeLocation(String fieldName, String fieldValue) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    int result = database.delete("Location", fieldName + " = ?",
        new String[] { fieldValue });
    return result;
  }

}




Java Source Code List

com.flyingboba.flyingdb.DatabaseAdapter.java
com.flyingboba.flyingdb.DatabaseHelper.java
com.flyingboba.flyingdb.PrefixEvent.java
com.flyingboba.flyingdb.PrefixLocation.java
com.flyingboba.flyingdb.PrefixPerson.java
com.flyingboba.flyingdbexample.MainActivity.java