0

我正在创建 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);

}

显然我做错了什么,我不太明白是什么。请帮忙。

4

1 回答 1

0

您声明所有列不为空,并且当您插入时,populateDB2您仅在 2 列中插入值,其他列则为空,因此约束失败。
还有为什么 3 个表,如果这些都是你的列,那么 1 个表就可以了。

于 2013-04-13T03:32:28.607 回答