1

I keep getting this error, i checked everything but can't find the problem. I'm trying to fill a database table with some test data and than populate a spinner with this data. Below is my code that is in the onCreate of the activity:

courseSpinner = (Spinner) findViewById(R.id.spinCourse);

    SQLiteHelperCourse courseDbHelper = new SQLiteHelperCourse(this);
    SQLiteDatabase dbw = courseDbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "1");
    values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "Android");
    values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
    values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
    values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
    dbw.insert(CourseEntry.TABLE_NAME, CourseEntry.COLUMN_NAME_NULLABLE, values);

    SQLiteDatabase dbr = courseDbHelper.getReadableDatabase();
    String[] projection = { CourseEntry.COLUMN_NAME_COURSE_NAME};
    final ArrayList<String> list = new ArrayList<String>();

    Cursor curs = dbr.query(CourseEntry.TABLE_NAME, 
            projection, 
            null, 
            null, 
            null, 
            null, 
            null);
    curs.moveToFirst();

    while(curs.moveToNext())
    {
        list.add(curs.getString(curs.getColumnIndexOrThrow(CourseEntry.COLUMN_NAME_COURSE_NAME)));
    }

    final StableArrayAdapter adapter = new StableArrayAdapter(this,
            android.R.layout.simple_spinner_item, list);

    courseSpinner.setAdapter(adapter);

Here is the contract class:

public final class CourseContract {

public CourseContract() {}

public static abstract class CourseEntry implements BaseColumns {

    public static final String TABLE_NAME = "courses";
    public static final String COLUMN_NAME_COURSE_ID = "courseid";
    public static final String COLUMN_NAME_COURSE_NAME = "coursename";
    public static final String COLUMN_NAME_YEAR = "year";
    public static final String COLUMN_NAME_SEMESTER = "semester";
    public static final String COLUMN_NAME_CREDITS = "credits";
    public static final String COLUMN_NAME_NULLABLE = "null";

}

}

Here is the SQLiteHelper class:

public class SQLiteHelperCourse extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";

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

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES = 
        "CREATE TABLE " + CourseEntry.TABLE_NAME + " (" + CourseEntry._ID + " INTEGER PRIMARY KEY, "
                                                        + CourseEntry.COLUMN_NAME_COURSE_ID + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_COURSE_NAME + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_YEAR + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_SEMESTER + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_CREDITS + TEXT_TYPE + ")";

private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + CourseEntry.TABLE_NAME;

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

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onUpgrade(db, oldVersion, newVersion);
}
}
Bouss
  • 205
  • 2
  • 9
  • 20
  • 1
    If you place a break point, at the line db.execSQL(SQL_CREATE_ENTRIES) does it being called/reached? – Shmil The Cat Dec 16 '14 at 22:38
  • ... all these string concatenations in your table creation! WHY? they're horribly heavy! And render a worse code readability, too. – Phantômaxx Dec 17 '14 at 08:12
  • 1
    Have you already uninstalled your app to ensure any possible older version of the database file is removed first? http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto Dec 17 '14 at 08:31
  • I was indeed using an older version of the app, I re-installed it and it works find now! – Bouss Dec 17 '14 at 14:15

0 Answers0