开发者

SQLiteConstraintException on inserting values in table

开发者 https://www.devze.com 2023-03-30 03:28 出处:网络
I am new to android development and I have an android database table with following code public class SoftCopyDatabase extends SQLiteOpenHelper {

I am new to android development and I have an android database table with following code

public class SoftCopyDatabase extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "SoftCopy.db";
private static final int DATABASE_VERSION = 1;

public SoftCopyDatabase(Context ctx) {
    super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SUBJECT
            + " TEXT NOT NULL," + TOPIC + " TEXT NOT NULL, "
            + LECTURENUMBER + " TEXT NOT NULL, " + PAGENUMBER
            + " TEXT NOT NULL, " + DATE + " TEXT NOT NULL, " + _DATA
            + " TEXT NOT NULL);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}} 

I am trying to add to this table using the method addInfo(), which is as follows:

private void addInfo(){
    ContentValues values = new ContentValues();
    values.put(SUBJECT, SaveData._subject);
    values.put(TOPIC, SaveData._topic);
    values.put(LECTURENUMBER, SaveData._lecturenumber);
    values.put(PAGENUMBER, Integer.toString(PAGE_NUMBER));
    values.put(DATE, SaveData._date);
    values.put(_DATA, SaveData.FILE_NAME + ".png");
    softCopyDB.getWritableDatabase().insertOrThrow(TABLE_NAME, null, values);

}

But when I increase the value of PAGE_NUMBER( Page_NUMBER is an Integer) while keeping all other entries same and call addInfo(), I receive an error at softCopyDB.getWritableDatabase().insertOrThrow(TABLE_NAME, null, values);

which is as follows

08-21 18:16:11.216: ERROR/AndroidRuntime(329): Caused by: android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 08-21 18:16:11.216: ERROR/AndroidRuntime(329): at android.database.sqlite.SQLiteStatement.native_execute(Native Method) 08-21 18:16:11.216: ERROR/AndroidRuntime(329开发者_运维百科): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55) 08-21 18:16:11.216: ERROR/AndroidRuntime(329): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549) 08-21 18:16:11.216: ERROR/AndroidRuntime(329): at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1432) 08-21 18:16:11.216: ERROR/AndroidRuntime(329): at neduet.telecom.softcopy.surface.LectureNoting.addInfo(LectureNoting.java:161)

Please help me in sorting this.


The only constraints I can see is PK and NOT NULL. As _ID is not part of your insert I would check your text values for null.

0

精彩评论

暂无评论...
验证码 换一张
取 消