开发者

SQLiteConstraintException: error code 19: constraint failed

I'm getting that exception when I do an insert in my SQLite database

The following code gives me the exception:

mDbHelper.createUser("pablo","a","a","a","a");

The code from mDbHelper (MyDbAdapter):

private static final String USER_TABLE_CREATE = "CREATE TABLE user ( email varchar, password varchar, fullName varchar, mobilePhone varchar, mobileOperatingSystem varchar, PRIMARY KEY  (email))";

public long createUser(String email, String password, String fullName, String mobilePhone, String mobileOperatingSystem) 
    {
    开发者_Go百科    ContentValues initialValues = new ContentValues();
        initialValues.put("email",email);
        initialValues.put("password",password);
        initialValues.put("fullName",fullName);
        initialValues.put("mobilePhone",mobilePhone);
        initialValues.put("mobileOperatingSystem",mobileOperatingSystem);
        return mDb.insert("user", null, initialValues);
    }

The exception is created on the last line: return mDb.insert("user", null, initialValues);


You are inserting a duplicate email.

Plus the recommended way is to have a _ID column as primary key, even if you don't use it. This way on future uses, like use in a Adapter or List, you won't have to workaround.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜