1

I my application crashes whenever I try and access data in my database. I am suspecting that I am not inserting the data correctly into the database, down below are the classes and methods I use.

public class MySQLiteHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "mydb.db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_QUICK_STOP = "quick_stop";

    public static String[] QUICK_STOP_FIELDS = {
        "_id", // autoincrement
        "quick_stop_stop_id", // stop id
        "quick_stop_name",
        "quick_stop_types", // ex. 1111 or 0101. Underground Train Bus Ferry, used to filter some specific stations
        "quick_stop_position"
        //TODO should there be a "stop_name" here?
    };

    private static final String DATABASE_CREATE = "create table "
        + TABLE_QUICK_STOP + "("
        + QUICK_STOP_FIELDS[0] + " long primary key, " // id (same stop can be added multiple times with different filters)
        + QUICK_STOP_FIELDS[1] + " integer not null, " // stop id
        + QUICK_STOP_FIELDS[2] + " varchar not null, " // stop name
        + QUICK_STOP_FIELDS[3] + " integer not null, " // stop types
        + QUICK_STOP_FIELDS[4] + " integer not null);"; // stop position            array[4] = cursor.getInt(4); // stop position

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUICK_STOP);
        onCreate(db);
    }
}

And I use this to insert:

public Boolean insertStop(int stopId, String name, int types, int position) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.QUICK_STOP_FIELDS[1], stopId);
    values.put(MySQLiteHelper.QUICK_STOP_FIELDS[2], name);
    values.put(MySQLiteHelper.QUICK_STOP_FIELDS[3], types);
    values.put(MySQLiteHelper.QUICK_STOP_FIELDS[4], position);
    long insertId = database.insert(MySQLiteHelper.TABLE_QUICK_STOP, null, values);
    if(insertId == -1)
        return false;
    return true;
}

As stated, Android Studio is horrible at giving information to be able to debug.

3
  • Did you alter the table structure after the first run? If so, change DATABASE_VERSION = 1; to DATABASE_VERSION = 2; or a higher number, in order to make onUpgrade() fire. Or try removing the db from the /data/data/... path in order for it to be re-created on next run. Commented Feb 7, 2015 at 20:11
  • @DerGolem I changed the database name and it worked, so you are correct in your comment. State it as an answer and I will accept! Commented Feb 7, 2015 at 20:19
  • Done! Sometimes errors hide in plain sight... Commented Feb 7, 2015 at 20:22

1 Answer 1

1

Did you alter the table structure after the first run?

If so, change DATABASE_VERSION = 1; to DATABASE_VERSION = 2; or a higher number, in order to make onUpgrade() fire.

Or, as an alternative, try removing (or renaming) the db from the /data/data/... path in order for it to be re-created on next run.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.