开发者

why does db.insert() giving the sqliteconstraintexception error 19 constraint failed

I have read most of the questions related to this exception but none of them are clear or indicative of why db.insert would throw this error. It was working fine without errors until I manually deleted the db from DDMS. Following is my SQLiteOpenHelper code:

public class LoginSQLiteOpenHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "logincredentials.sqlite";
public static final int DB_VERSION_NUMBER = 1;
public static final String DB_TABLE_NAME = "credentials";
public static final String USERNAME = "user_name";
public static final String PASSWORD = "password";

private static final String DB_CREATE_SCRIPT =  "create table " + DB_TABLE_NAME +
                    "( _id integer primary key autoincrement," +
  开发者_如何学Go                  USERNAME + " text not null, " +
                    PASSWORD + " text not null );" ;

public LoginSQLiteOpenHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION_NUMBER);

}

@Override
public void onCreate(SQLiteDatabase aSqliteDB) {
    Logger.d("Create", "Creating the database...");
    aSqliteDB.execSQL(DB_CREATE_SCRIPT);
}
}

My code for inserting the values is:

ContentValues contentValues = new ContentValues();
contentValues.put(LoginSQLiteOpenHelper.USERNAME, loginId);
contentValues.put(LoginSQLiteOpenHelper.PASSWORD, password);
database.insert(LoginSQLiteOpenHelper.DB_TABLE_NAME, null, contentValues);


This is why it occurred to me. If you declare one of your column name type as UNIQUE in your Create Table query in Database and try to insert a non unique variable, it invokes SQLiteConstraintException error.

A UNIQUE constraint is similar to a PRIMARY KEY constraint, except that a single table may have any number of UNIQUE constraints. For each UNIQUE constraint on the table, each row must feature a unique combination of values in the columns identified by the UNIQUE constraint. As with PRIMARY KEY constraints, for the purposes of UNIQUE constraints NULL values are considered distinct from all other values (including other NULLs). If an INSERT or UPDATE statement attempts to modify the table content so that two or more rows feature identical values in a set of columns that are subject to a UNIQUE constraint, it is a constraint violation. Source - http://www.sqlite.org/lang_createtable.html


I have read pretty much all forums looking for an exact reason for the occurrence of this exception. However, nowhere it clearly states so. However, by means of this code of mine, I can explain why it ocurred for me.

The code snippet I provided, is actually flawless. I am doing exactly what is required to do a db.insert().

However, i figured out the exception in 2 steps. 1. first time when i inserted values, I did not insert a value for the column Password. 2. second time, I added a value for column for Password, but due to incorrect passing of values it was null.

hence, I deduced from this exercise, that no column are allowed null values. You must initialize them with some value.

Please feel free to comment/add or correct me if I am wrong. I would like anyone else running into this issue to be clear on it as there are no good documentation on this exception.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜