OmiO OmiO - 1 month ago 6
Android Question

Multiple Tables with same Field, Failed Constraint error

I am creating 3 tables containing the following fields
the problem is there are Constraint failed errors while inserting data in table 2 and 3

TABLE 1 TABLE 2

|_id| name | class | sec | roll| |_id| class | sec | doa |

TABLE 3
|_id | class| sec | roll | aop | doa |


here is the code to generate the table

public static final String KEY_RID = "row_id";
public static final String KEY_NAME = "student_name";
public static final String KEY_ROLL = "student_roll";
public static final String KEY_SEC = "student_sect";
public static final String KEY_CLASS = "student_class";
public static final String KEY_DOA = "dayofattend";
public static final String KEY_AOP = "student_status";

private static final String DATABASE_NAME = "StudendtAttend";
private static final String DATABASE_TABLE = "AttendTable";
private static final String DATABASE_TABLE2 = "AttendDate";
private static final String DATABASE_TABLE3 = "AttendAop";

private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub

}

// Create Database
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_RID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ROLL
+ " TEXT NOT NULL, " + KEY_NAME + " TEXT NOT NULL, "
+ KEY_CLASS + " TEXT NOT NULL, " + KEY_SEC
+ " TEXT NOT NULL);");

db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + " (" + KEY_RID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CLASS
+ " TEXT NOT NULL, " + KEY_SEC + " TEXT NOT NULL, "
+ KEY_DOA + " TEXT NOT NULL);");

db.execSQL("CREATE TABLE " + DATABASE_TABLE3 + " (" + KEY_RID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CLASS
+ " TEXT NOT NULL, " + KEY_SEC + " TEXT NOT NULL, "
+ KEY_ROLL + " TEXT NOT NULL, " + KEY_DOA
+ " TEXT NOT NULL, " + KEY_AOP + " TEXT NOT NULL);");
}



// Insert Values into DB
public long populateDB(String roll, String name, String sec, String clas) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_ROLL, roll);
cv.put(KEY_NAME, name);
cv.put(KEY_CLASS, clas);
cv.put(KEY_SEC, sec);
ourDatabase.insert(DATABASE_TABLE, null, cv);
populateDB2(roll, name, sec, clas );
return (0);

}
public long populateDB2(String roll, String name, String sec, String clas) {
ContentValues cv1 = new ContentValues();
cv1.put(KEY_CLASS, clas);
cv1.put(KEY_SEC, sec);
ourDatabase.insert(DATABASE_TABLE2, null, cv1);

populateDB3(roll, name, sec, clas );
return 0;

}
public long populateDB3(String roll, String name, String sec, String clas) {
ContentValues cv2 = new ContentValues();
cv2.put(KEY_ROLL, roll);
cv2.put(KEY_CLASS, clas);
cv2.put(KEY_SEC, sec);
ourDatabase.insert(DATABASE_TABLE3, null, cv2);
return(0);

}


Obviously I am doing something wrong, I can't quite get what.
Help please.

Answer

You declare all the columns not null, and when you insert in populateDB2 you only insert values in only 2 columns the others are then null thus Constraint failed.
Also why 3 tables, if these are all your columns then 1 table is OK.