我正在创建包含以下字段的3个表 问题是在表2和3中插入数据时存在约束失败错误
TABLE 1 TABLE 2
|_id| name | class | sec | roll| |_id| class | sec | doa |
TABLE 3
|_id | class| sec | roll | aop | doa |
这是生成表的代码
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);
}
显然我做错了什么,我不太明白。 请帮助。
答案 0 :(得分:0)
您声明所有列都不为空,当您在populateDB2
中插入时,您只在2列中插入值,其他列则为null,因此约束失败。
另外为什么3个表,如果这些都是你的列,那么1个表就可以了。