0

I am trying to create an SQLite database which contains two tables. The second table (days_table) is supposed to hold data for a List, so that needs to be connected to the first table through a foreign key. (Not sure if I've done it right.)

EDIT: updated code:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "alarms.db";
    private static final String TABLE_NAME = "alarm_table";
    private static final String COL_1 = "ID";
    private static final String COL_2 = "NAME";
    //private static final String COL_3 = "DAYS";
    private static final String COL_4 = "RINGTONE";
    private static final String COL_5 = "HOUR";
    private static final String COL_6 = "MINUTE";

    private static final String TABLE_NAME_DAYS = "days_table";
    private static final String DAYS_COL_1 = "MONDAY";
    private static final String DAYS_COL_2 = "TUESDAY";
    private static final String DAYS_COL_3 = "WEDNESDAY";
    private static final String DAYS_COL_4 = "THURSDAY";
    private static final String DAYS_COL_5 = "FRIDAY";
    private static final String DAYS_COL_6 = "SATURDAY";
    private static final String DAYS_COL_7 = "SUNDAY";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 2);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE "  + TABLE_NAME + " ("
            + COL_1 + " TEXT PRIMARY KEY NOT NULL, "
            + COL_2 + " TEXT NOT NULL, "
            + COL_4 + " TEXT NOT NULL, "
            + COL_5 + " INTEGER NOT NULL, "
            + COL_6 + " INTEGER NOT NULL" +
    ")");

    db.execSQL("CREATE TABLE "  + TABLE_NAME_DAYS + " ("
            + COL_1 + " TEXT PRIMARY KEY NOT NULL, "
            + DAYS_COL_1 + " TEXT, "
            + DAYS_COL_2 + " TEXT, "
            + DAYS_COL_3 + " TEXT, "
            + DAYS_COL_4 + " TEXT, "
            + DAYS_COL_5 + " TEXT, "
            + DAYS_COL_6 + " TEXT, "
            + DAYS_COL_7 + " TEXT "
            +
    ")");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

}

public boolean addObjectToDB(Alarm inData){
    SQLiteDatabase db = getWritableDatabase();
    boolean rValue;
    long result;

    // ============== CV : start ==============

    ContentValues cv = new ContentValues();

    cv.put(COL_1, inData.getId());
    cv.put(COL_2, inData.getName());
    //     COL_3 - DAYS
    cv.put(COL_4, inData.getRingtone().toString());
    cv.put(COL_5, inData.getHour());
    cv.put(COL_6, inData.getMinute());

    result = db.insert(TABLE_NAME, null, cv);
    if(result == -1){
        rValue = false;
    }
    else{
        // ============== CV2 : start ==============

        ContentValues cv2 = new ContentValues();
        List<String> days = inData.getDays();

        cv2.put(COL_1, inData.getId());
        cv2.put(DAYS_COL_1, days.get(0));
        cv2.put(DAYS_COL_2, days.get(1));
        cv2.put(DAYS_COL_3, days.get(2));
        cv2.put(DAYS_COL_4, days.get(3));
        cv2.put(DAYS_COL_5, days.get(4));
        cv2.put(DAYS_COL_6, days.get(5));
        cv2.put(DAYS_COL_7, days.get(6));

        result = db.insert(TABLE_NAME_DAYS, null, cv2);

        rValue = result != -1;
    }

    return rValue;
}
}

I updated my code, as seen above. Now I get the following error instead:

Error inserting SATURDAY=null FRIDAY=null THURSDAY=null TUESDAY=Tue SUNDAY=null MONDAY=null WEDNESDAY=null ID=Alarm_ade731f0-f5e4-44c2-9c4a-9f35eaf8941e

android.database.sqlite.SQLiteException: no such table: days_table(Sqlite code 1): , while compiling: INSERT INTO days_table(SATURDAY,FRIDAY,THURSDAY,TUESDAY,SUNDAY,MONDAY,WEDNESDAY,ID) VALUES (?,?,?,?,?,?,?,?),(OS error - 2:No such file or directory)

It seems as though I have an error in my second Create Table sql query?

Community
  • 1
  • 1
  • Your table does not have a column named `DAYS_COL_2`; that column is named `TUESDAY`. (`DAYS_COL_2` is the name of the string object whose value contains the name of the column.) – CL. Sep 24 '17 at 12:13
  • @CL. You're right. I have updated my code, but I still get an error. See the original post for details. –  Sep 24 '17 at 16:07

0 Answers0