简体   繁体   English

SQLiteDatabase的onCreate()和onUpgrade()没有被调用

[英]SQLiteDatabase onCreate() and onUpgrade() not being called

I'm developing an app that (so far) pulls data from an API, inserts it to a local SQLite database & displays it on screen. 我正在开发一个应用程序(到目前为止),该应用程序从API提取数据,将其插入本地SQLite数据库并在屏幕上显示。

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 . 为了简化我的生活,我根据这个问题的最高答案写了一个主数据库适配器(MyDBAdapter)以及每个表的适配器。

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). 在开发该应用程序时,我还在自学JUnit中的单元测试(不知道这是否相关,但我认为我会把它扔在那里)。

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. 当我增加DB_VERSION值时,不会调用onUpgrade() ,并且由于尝试访问不存在的表,应用程序会出错。

What circumstances would cause onUpgrade() to not get called? 什么情况下会导致onUpgrade()无法调用? I'm at my wits' end trying to figure this out. 我正竭尽全力试图解决这个问题。

MyDbAdapter: 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: 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);
    }
}

Android would not be able to figure out that it needs to run MyDbAdapter on its own. Android无法确定它需要自己运行MyDbAdapter。

Create a class that extends Application. 创建一个扩展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: 在AndroidManifest.xml上,您需要像这样在application元素中设置属性:

<application
    android:name=".MyApplication"
...
/application>

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. 我没有使* Adapter类彼此独立,而是修改了代码以使它们成为MyDBAdapter的子代。 This way, when open() is called, it calls the open() method of MyDBAdapter, checking the DB version & upgrading when appropriate. 这样,当open()被调用时,它调用open() MyDBAdapter的方法,检查数据库版本和升级在适当的时候。

MyDbAdapter: 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: 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM