0

I'm developing an app that (so far) pulls data from an API, inserts it to a local SQLite database & displays it on screen.

In order to make my life simpler, I wrote a master database adapter (MyDBAdapter) as well as adapters for each individual table, according to the top anser for this question.

While developing the app, I'm also teaching myself unit testing in JUnit (not sure if this is relevant, but I figured I'd throw it in there).

When trying to refresh the database I somehow dropped all the tables and now I can't get them back. When I increment the DB_VERSION value, onUpgrade() doesn't get called, and the app errors out because it's trying to access nonexistent tables.

What circumstances would cause onUpgrade() to not get called? I'm at my wits' end trying to figure this out.

MyDbAdapter:

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


public class MyDbAdapter {
    public static final String TAG = "MyDbAdapter";
    protected static final String DB_NAME = "mydb.db";
    protected static final int DB_VERSION = 21;

    private final Context context;
    private DbHelper helper;
    private SQLiteDatabase db;

    private static final String CREATE_TABLE_CRUISE_LINES = "create table " + CruiseLineAdapter.TABLE + " (" + CruiseLineAdapter.C_ID + " integer primary key autoincrement, "
            + CruiseLineAdapter.C_NAME + " TEXT);";
    private static final String CREATE_TABLE_SHIPS = "create table " + ShipAdapter.TABLE + " (" + ShipAdapter.C_ID + " integer primary key autoincrement, "
            + ShipAdapter.C_NAME + " TEXT, "
            + ShipAdapter.C_CRUISE_LINE + " integer);";
    private static final String CREATE_TABLE_TABLE_UPDATES = "create table " + UpdateTimestampAdapter.TABLE + " (" + UpdateTimestampAdapter.C_ID + " integer primary key autoincrement, "
            + UpdateTimestampAdapter.C_TABLE_NAME + " TEXT, "
            + UpdateTimestampAdapter.C_LAST_UPDATE + " TEXT);";

    private static final String DROP_TABLE = "drop table if exists %s";
    private static final String DROP_TABLE_CRUISE_LINES = String.format(DROP_TABLE, CruiseLineAdapter.TABLE);
    private static final String DROP_TABLE_SHIPS = String.format(DROP_TABLE, ShipAdapter.TABLE);
    private static final String DROP_TABLE_TABLE_UPDATES = String.format(DROP_TABLE, UpdateTimestampAdapter.TABLE);

    public MyDbAdapter (Context context) {
        this.context = context;
        helper = new DbHelper(this.context);
    }

    private static class DbHelper extends SQLiteOpenHelper {
        DbHelper (Context context) {
            super(context, DB_NAME, null, DB_VERSION);
            Log.i(TAG, "initialized");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.i(TAG, "Database created: version " + DB_VERSION);
            db.execSQL(CREATE_TABLE_CRUISE_LINES);
            db.execSQL(CREATE_TABLE_SHIPS);
            db.execSQL(CREATE_TABLE_TABLE_UPDATES);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.i(TAG, "Database upgraded to " + DB_VERSION);
            db.execSQL(DROP_TABLE_CRUISE_LINES);
            db.execSQL(DROP_TABLE_SHIPS);
            db.execSQL(DROP_TABLE_TABLE_UPDATES);

            this.onCreate(db);
        }
    }

    public MyDbAdapter open() throws SQLException {
        db = helper.getWritableDatabase();
        return this;
    }

    public void close() {
        helper.close();
    }
}

ShipAdapter:

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.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class ShipAdapter {
    public static final String TAG = "ShipAdapter";
    public static final String TABLE = "ships";

    public static final String C_ID = BaseColumns._ID;
    public static final String C_NAME = "name";
    public static final String C_CRUISE_LINE = "cruise_line";

    private DbHelper dbHelper;
    private SQLiteDatabase db;

    private final Context context;

    private static class DbHelper extends SQLiteOpenHelper {
        DbHelper (Context context) {
            super(context, MyDbAdapter.DB_NAME, null, MyDbAdapter.DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }

    public ShipAdapter(Context context) {
        this.context = context;
    }

    public ShipAdapter open() throws SQLException {
        dbHelper = new DbHelper(context);
        db = dbHelper.getWritableDatabase();

        return this;
    }

    public void close() {
        dbHelper.close();
    }

    public long createShip(String name, long cruise_line_id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(C_NAME, name);
        initialValues.put(C_CRUISE_LINE, cruise_line_id);

        return db.insert(TABLE, null, initialValues);
    }

    public long createShip(long id, String name, long cruise_line_id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(C_ID, id);
        initialValues.put(C_NAME, name);
        initialValues.put(C_CRUISE_LINE, cruise_line_id);

        return db.insert(TABLE, null, initialValues);
    }

    public long createShip(ShipModel ship) {
        return createShip(ship.getName(), ship.getCruiseLineId());
    }

    public long insertOrIgnoreShip(long id, String name, long cruise_line_id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(C_ID, id);
        initialValues.put(C_NAME, name);
        initialValues.put(C_CRUISE_LINE, cruise_line_id);

        return db.insertWithOnConflict(TABLE, null, initialValues, SQLiteDatabase.CONFLICT_IGNORE);
    }

    public long insertOrIgnoreShip(ShipModel ship) {
        return insertOrIgnoreShip(ship.getId(), ship.getName(), ship.getCruiseLineId());
    }

    public List<ShipModel> getAllShips() {
        List<ShipModel> ships = new ArrayList<ShipModel>();
        Cursor cursor = getAllShipsCursor();

        if (cursor.getCount() > 0) {
            while(!cursor.isAfterLast()) {
                ships.add(cursorToShip(cursor));
                cursor.moveToNext();
            }
        }

        return ships;
    }

    public Cursor getAllShipsCursor() {
        Cursor cursor = db.query(TABLE, null, null, null, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
        }

        return cursor;
    }

    public ShipModel getShip(long id) {
        Cursor cursor = getShipCursor(id);

        if (cursor.getCount() > 0) {
            return cursorToShip(cursor);
        }

        return null;
    }

    public Cursor getShipCursor(long id) {
        Cursor cursor = db.query(TABLE, null, C_ID + " = ?", new String[] { String.valueOf(id) }, null, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
        }

        return cursor;
    }

    public ShipModel getShip(String name) {
        Cursor cursor = getShipCursor(name);

        if (cursor.getCount() > 0) {
            return cursorToShip(cursor);
        }

        return null;
    }

    public Cursor getShipCursor(String name) {
        Cursor cursor = db.query(TABLE, null, C_NAME + " = ?", new String[] { name }, null, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
        }

        return cursor;
    }

    public List<ShipModel> getShipsByCruiseLine(long cruise_line_id) {
        List<ShipModel> ships = new ArrayList<ShipModel>();
        Cursor cursor = getShipsCursorByCruiseLine(cruise_line_id);

        if (cursor.getCount() > 0) {
            while (!cursor.isAfterLast()) {
                ships.add(cursorToShip(cursor));
                cursor.moveToNext();
            }
        }

        return ships;
    }

    public Cursor getShipsCursorByCruiseLine(long cruise_line_id) {
        Cursor cursor = db.query(TABLE, null, C_CRUISE_LINE + " = ?", new String[] { String.valueOf(cruise_line_id) }, null, null, null, null);

        if (cursor.getCount() > 0) { 
            cursor.moveToFirst();
        }

        return cursor;
    }

    public boolean updateShip(long id, String name, long cruise_line_id) {
        ContentValues args = new ContentValues();
        args.put(C_NAME, name);
        args.put(C_CRUISE_LINE, cruise_line_id);

        return db.update(TABLE, args, C_ID + " = ?", new String[] { String.valueOf(id) }) > 0;
    }

    public boolean updateShip(ShipModel ship) {
        return updateShip(ship.getId(), ship.getName(), ship.getCruiseLineId());
    }

    public boolean deleteShip(long id) {
        return db.delete(TABLE, C_ID + " = ?", new String[] { String.valueOf(id) }) > 0;
    }

    public boolean deleteShip(String name) {
        return db.delete(TABLE, C_NAME + " = ?", new String[] { name }) > 0;
    }

    public boolean deleteShip(ShipModel ship) {
        return deleteShip(ship.getName());
    }

    public boolean deleteAll() {
        return db.delete(TABLE, null, null) > 0;
    }

    private ShipModel cursorToShip(Cursor cursor) {
        long id = cursor.getLong(cursor.getColumnIndex(C_ID));
        String name = cursor.getString(cursor.getColumnIndex(C_NAME));
        long cruise_line_id = cursor.getLong(cursor.getColumnIndex(C_CRUISE_LINE));

        return new ShipModel(id, name, cruise_line_id);
    }
}

Communicator:

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

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NoHttpResponseException;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by mcampo on 5/30/13.
 */
public class Communicator {
    private static final String TAG = "Communicator";
    private static final int CONNECTION_TIMEOUT = 10000;
    protected String base_url = "http://[myapi]/mobileapi/";
    protected Context context;
    protected CruiseLineAdapter cruise_line_adapter;
    protected ShipAdapter ship_adapter;
    protected UpdateTimestampAdapter table_update_adapter;
    protected String update_timestamps_json;

    public Communicator() {

    }

    public Communicator (Context context) {
        this.context = context;
        this.cruise_line_adapter = new CruiseLineAdapter(this.context);
        this.ship_adapter = new ShipAdapter(this.context);
        this.table_update_adapter = new UpdateTimestampAdapter(this.context);
    }

    // begin defining getters / setters

    /**
     *
     * @param context
     */
    public void setContext(Context context) {
        this.context = context;
        this.cruise_line_adapter = new CruiseLineAdapter(this.context);
        this.ship_adapter = new ShipAdapter(this.context);
        this.table_update_adapter = new UpdateTimestampAdapter(this.context);
    }

    public Context getContext() {
        return this.context;
    }

    // end getters / setters

    private boolean isNetworkConnected() {
        if (context == null) {
            return false;
        }
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null;
    }

    private String makeApiCall(String api_extension) throws IOException {
        if (!isNetworkConnected()) {
            throw new IOException("Your device is not connected to the internet. Please enable your network connection and restart CabinGuru.");
        }

        Log.d(TAG, "Making HTTP request to " + this.base_url + api_extension);

        HttpClient httpClient = new DefaultHttpClient();
        HttpParams params = httpClient.getParams();

        HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);

        HttpResponse response = httpClient.execute(new HttpGet(this.base_url + api_extension));

        StatusLine statusLine = response.getStatusLine();

        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();

            Log.i(TAG, "HTTP Response: " + out.toString());

            return out.toString();
        } else {
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    }


    private boolean tableIsUpToDate(String table) throws IOException {
        try {
            String api_timestamp = getApiUpdateTimestamp(table);
            String device_timestamp = getDeviceUpdateTimestamp(table);

            if (device_timestamp == null || device_timestamp.equals("")) {
                throw new NullPointerException("device_timestamp is null");
            }

            Log.i(TAG, "API Timestamp: " + api_timestamp);
            Log.i(TAG, "Device Timestamp: " + device_timestamp);

            // compare device_timestamp to api_timestamp. If device_timestamp comes after api_timestamp, table is up-to-date.
            DateTime api_datetime = this.strToDateTime(api_timestamp);
            DateTime device_datetime = this.strToDateTime(device_timestamp);

            return device_datetime.isAfter(api_datetime);
        } catch (NullPointerException e) {
            e.printStackTrace();
            Log.e(TAG, "NullPointerException encountered in tableIsUpToDate(" + table + "): " + e.getMessage() + " " + e.getCause());
            return false;
        }
    }

    private String getDeviceUpdateTimestamp(String table) {
        String return_string = "";
        table_update_adapter.open();
        UpdateTimestampModel timestamp = this.table_update_adapter.getUpdateTimestamp(table);
        table_update_adapter.close();

        try {
            return_string = timestamp.getLastUpdate();

            return return_string;
        } catch (NullPointerException e) {
            Log.e(TAG, "NullPointerException encountered in getDeviceUpdateTimestamp(" + table + "): " + e.getMessage());
            return "";
        }
    }

    private boolean updateLastUpdateTimestamp(String table) {
        // set up current timestamp
        DateTime timestamp = new DateTime(System.currentTimeMillis());
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

        String now = formatter.print(timestamp);

        // fetch ID of row to update
        table_update_adapter.open();
        table_update_adapter.updateOrCreateTimestamp(table,  now);
        table_update_adapter.close();

        return true;
    }

    private void getApiUpdateTimestamps() throws IOException {
        if (this.update_timestamps_json == null || this.update_timestamps_json.equals(""))
            try {
                this.update_timestamps_json = this.makeApiCall("get_update_timestamps");
            } catch (NoHttpResponseException e) {
                Log.e(TAG, "App was unable to connect to the servers.");
            }
    }

    private String getApiUpdateTimestamp(String table) throws IOException {
        this.getApiUpdateTimestamps();

        try {
            if (this.update_timestamps_json == null) {
                throw new Exception("Could not fetch update timestamps. Check and make sure you are able to connect to " + this.base_url + ".");
            }
            JSONObject timestamps = new JSONObject(this.update_timestamps_json);

            return timestamps.getString(table);
        } catch (JSONException e) {
            Log.e(TAG, "An error occurred when extracting update timestamps from the api: " + e.getMessage() + " | " + e.getCause());
            return null;
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return null;
        }
    }

    public boolean updateCruiseLines() throws IOException {
        // if the cruise lines from the API have been updated since the last update on the device, update the device.
        if (!this.tableIsUpToDate(CruiseLineAdapter.TABLE)) {
            Log.i(TAG, "Attempting API call for Cruise Lines.");

            try {
                String cruise_line_json = this.makeApiCall("cruise_lines");

                JSONArray cruise_lines = new JSONArray(cruise_line_json);

                // loop through cruise_lines, add to database
                int array_size = cruise_lines.length();
                cruise_line_adapter.open();
                for (int i = 0; i < array_size; i++) {
                    JSONObject cruise_line = cruise_lines.getJSONObject(i);

                    int cruise_line_id = cruise_line.getInt("CruiseLineID");
                    String cruise_line_name = cruise_line.getString("Name");

                    // insert record into database.
                    this.cruise_line_adapter.insertOrIgnoreCruiseLine(cruise_line_id,  cruise_line_name);
                }
                cruise_line_adapter.close();

                this.updateLastUpdateTimestamp(CruiseLineAdapter.TABLE);
            } catch (JSONException e) {
                Log.e(TAG, "JSONException encountered in updateCruiseLines(): " + e.getMessage());
                e.printStackTrace();
                return false;
            }
        } else {
            Log.i(TAG, "Cruise Line records exist. No API call necessary.");
        }

        return true;
    }

    public boolean updateShips() throws IOException {
        // if the ships from the API have been updated since the last update on the device, update the device
        if (!this.tableIsUpToDate(ShipAdapter.TABLE)) {
            Log.i(TAG, "Attempting API call for Ships.");

            try {
                String ships_json = this.makeApiCall("ships");

                JSONArray ships = new JSONArray(ships_json);

                // loop through ships, add to database
                int array_size = ships.length();
                ship_adapter.open();
                for (int i = 0; i < array_size; i++) {
                    JSONObject ship = ships.getJSONObject(i);

                    int id = ship.getInt("ShipID");
                    String name = ship.getString("ShipName");
                    int cruise_line_id = ship.getInt("CruiseLineID");

                    this.ship_adapter.insertOrIgnoreShip(id, name, cruise_line_id);
                }
                ship_adapter.close();

                this.updateLastUpdateTimestamp(ShipAdapter.TABLE);
            } catch (JSONException e) {
                Log.e(TAG, "JSONException encountered in updateShips():" + e.getMessage());
                e.printStackTrace();
                return false;
            }
        } else {
            Log.i(TAG, "Ship records exist. No API call necessary.");
        }

        return true;
    }

    private DateTime strToDateTime(String timestamp) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        return formatter.parseDateTime(timestamp);
    }
}
2
  • onCreate() should be called when it's creating the database for the first time (the file doesn't already exist). onUpgrade() should be called when the existing database version is less than the version your app uses when opening the database. Not sure if you've tried this, but on emulator you can access the db on the filesystem at /data/data/your.package.name/databases/your_db_name. You might just remove those files completely to force creation again. If nothing else, maybe uninstall and reinstall the app--should have the same effect. If that doesn't work, then I have no idea.
    – Karakuri
    Jun 18, 2013 at 19:55
  • @Karakuri Tried, and failed. In an effort to figure this out I have uninstalled the app more times than I can count.
    – Matt
    Jun 18, 2013 at 19:58

2 Answers 2

0

Android would not be able to figure out that it needs to run MyDbAdapter on its own.

Create a class that extends Application.

public class MyApplication extends Application {
    private static MyDbAdapter dbAdapter;

@Override
public void onCreate() {
        super.onCreate();
        dbAdapter = new MyDbAdapter(getApplicationContext());
    }
}

On your AndroidManifest.xml, you need to set the attribute in the application element like this:

<application
    android:name=".MyApplication"
...
/application>
3
  • It was working fine up until a few hours prior to my post...I might just need to step back from it for a day.
    – Matt
    Jun 18, 2013 at 20:20
  • Is it being instantiated on Application onCreate?
    – dannyroa
    Jun 18, 2013 at 20:34
  • Yes, in the onCreate method of the main activity.
    – Matt
    Jun 18, 2013 at 20:36
0

After stepping away from the problem for a few hours I came up with this solution:

Instead of making the *Adapter classes independent of one another, I modified the code to make them children of MyDBAdapter. This way, when open() is called, it calls the open() method of MyDBAdapter, checking the DB version & upgrading when appropriate.

MyDbAdapter:

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


public class MyDbAdapter {
    public static final String TAG = "MyDbAdapter";
    protected static final String DB_NAME = "mydb.db";
    protected static final int DB_VERSION = 21;

    private final Context context;
    private DbHelper helper;
    private SQLiteDatabase db;

    private static final String CREATE_TABLE_CRUISE_LINES = "create table " + CruiseLineAdapter.TABLE + " (" + CruiseLineAdapter.C_ID + " integer primary key autoincrement, "
            + CruiseLineAdapter.C_NAME + " TEXT);";
    private static final String CREATE_TABLE_SHIPS = "create table " + ShipAdapter.TABLE + " (" + ShipAdapter.C_ID + " integer primary key autoincrement, "
            + ShipAdapter.C_NAME + " TEXT, "
            + ShipAdapter.C_CRUISE_LINE + " integer);";
    private static final String CREATE_TABLE_TABLE_UPDATES = "create table " + UpdateTimestampAdapter.TABLE + " (" + UpdateTimestampAdapter.C_ID + " integer primary key autoincrement, "
            + UpdateTimestampAdapter.C_TABLE_NAME + " TEXT, "
            + UpdateTimestampAdapter.C_LAST_UPDATE + " TEXT);";

    private static final String DROP_TABLE = "drop table if exists %s";
    private static final String DROP_TABLE_CRUISE_LINES = String.format(DROP_TABLE, CruiseLineAdapter.TABLE);
    private static final String DROP_TABLE_SHIPS = String.format(DROP_TABLE, ShipAdapter.TABLE);
    private static final String DROP_TABLE_TABLE_UPDATES = String.format(DROP_TABLE, UpdateTimestampAdapter.TABLE);

    public MyDbAdapter (Context context) {
        this.context = context;
        helper = new DbHelper(this.context);
    }

    private static class DbHelper extends SQLiteOpenHelper {
        DbHelper (Context context) {
            super(context, DB_NAME, null, DB_VERSION);
            Log.i(TAG, "initialized");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.i(TAG, "Database created: version " + DB_VERSION);
            db.execSQL(CREATE_TABLE_CRUISE_LINES);
            db.execSQL(CREATE_TABLE_SHIPS);
            db.execSQL(CREATE_TABLE_TABLE_UPDATES);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.i(TAG, "Database upgraded to " + DB_VERSION);
            db.execSQL(DROP_TABLE_CRUISE_LINES);
            db.execSQL(DROP_TABLE_SHIPS);
            db.execSQL(DROP_TABLE_TABLE_UPDATES);

            this.onCreate(db);
        }
    }

    public MyDbAdapter open() throws SQLException {
        db = helper.getWritableDatabase();
        return this;
    }

    public void close() {
        helper.close();
    }
}

ShipAdapter:

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.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class ShipAdapter extends MyDbAdapter {
    public static final String TAG = "ShipAdapter";
    public static final String TABLE = "ships";

    public static final String C_ID = BaseColumns._ID;
    public static final String C_NAME = "name";
    public static final String C_CRUISE_LINE = "cruise_line";

    public ShipAdapter(Context context) {
        this.context = context;
    }

    public long createShip(String name, long cruise_line_id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(C_NAME, name);
        initialValues.put(C_CRUISE_LINE, cruise_line_id);

        return db.insert(TABLE, null, initialValues);
    }

    public long createShip(long id, String name, long cruise_line_id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(C_ID, id);
        initialValues.put(C_NAME, name);
        initialValues.put(C_CRUISE_LINE, cruise_line_id);

        return db.insert(TABLE, null, initialValues);
    }

    public long createShip(ShipModel ship) {
        return createShip(ship.getName(), ship.getCruiseLineId());
    }

    public long insertOrIgnoreShip(long id, String name, long cruise_line_id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(C_ID, id);
        initialValues.put(C_NAME, name);
        initialValues.put(C_CRUISE_LINE, cruise_line_id);

        return db.insertWithOnConflict(TABLE, null, initialValues, SQLiteDatabase.CONFLICT_IGNORE);
    }

    public long insertOrIgnoreShip(ShipModel ship) {
        return insertOrIgnoreShip(ship.getId(), ship.getName(), ship.getCruiseLineId());
    }

    public List<ShipModel> getAllShips() {
        List<ShipModel> ships = new ArrayList<ShipModel>();
        Cursor cursor = getAllShipsCursor();

        if (cursor.getCount() > 0) {
            while(!cursor.isAfterLast()) {
                ships.add(cursorToShip(cursor));
                cursor.moveToNext();
            }
        }

        return ships;
    }

    public Cursor getAllShipsCursor() {
        Cursor cursor = db.query(TABLE, null, null, null, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
        }

        return cursor;
    }

    public ShipModel getShip(long id) {
        Cursor cursor = getShipCursor(id);

        if (cursor.getCount() > 0) {
            return cursorToShip(cursor);
        }

        return null;
    }

    public Cursor getShipCursor(long id) {
        Cursor cursor = db.query(TABLE, null, C_ID + " = ?", new String[] { String.valueOf(id) }, null, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
        }

        return cursor;
    }

    public ShipModel getShip(String name) {
        Cursor cursor = getShipCursor(name);

        if (cursor.getCount() > 0) {
            return cursorToShip(cursor);
        }

        return null;
    }

    public Cursor getShipCursor(String name) {
        Cursor cursor = db.query(TABLE, null, C_NAME + " = ?", new String[] { name }, null, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
        }

        return cursor;
    }

    public List<ShipModel> getShipsByCruiseLine(long cruise_line_id) {
        List<ShipModel> ships = new ArrayList<ShipModel>();
        Cursor cursor = getShipsCursorByCruiseLine(cruise_line_id);

        if (cursor.getCount() > 0) {
            while (!cursor.isAfterLast()) {
                ships.add(cursorToShip(cursor));
                cursor.moveToNext();
            }
        }

        return ships;
    }

    public Cursor getShipsCursorByCruiseLine(long cruise_line_id) {
        Cursor cursor = db.query(TABLE, null, C_CRUISE_LINE + " = ?", new String[] { String.valueOf(cruise_line_id) }, null, null, null, null);

        if (cursor.getCount() > 0) { 
            cursor.moveToFirst();
        }

        return cursor;
    }

    public boolean updateShip(long id, String name, long cruise_line_id) {
        ContentValues args = new ContentValues();
        args.put(C_NAME, name);
        args.put(C_CRUISE_LINE, cruise_line_id);

        return db.update(TABLE, args, C_ID + " = ?", new String[] { String.valueOf(id) }) > 0;
    }

    public boolean updateShip(ShipModel ship) {
        return updateShip(ship.getId(), ship.getName(), ship.getCruiseLineId());
    }

    public boolean deleteShip(long id) {
        return db.delete(TABLE, C_ID + " = ?", new String[] { String.valueOf(id) }) > 0;
    }

    public boolean deleteShip(String name) {
        return db.delete(TABLE, C_NAME + " = ?", new String[] { name }) > 0;
    }

    public boolean deleteShip(ShipModel ship) {
        return deleteShip(ship.getName());
    }

    public boolean deleteAll() {
        return db.delete(TABLE, null, null) > 0;
    }

    private ShipModel cursorToShip(Cursor cursor) {
        long id = cursor.getLong(cursor.getColumnIndex(C_ID));
        String name = cursor.getString(cursor.getColumnIndex(C_NAME));
        long cruise_line_id = cursor.getLong(cursor.getColumnIndex(C_CRUISE_LINE));

        return new ShipModel(id, name, cruise_line_id);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.