SQLiteConstraintException:错误代码19:约束失败

时间:2021-02-01 05:33:12

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

我在SQLite数据库中插入时遇到异常

The following code gives me the exception:

以下代码给出了例外情况:

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

The code from mDbHelper (MyDbAdapter):

来自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) 
    {
        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);

在最后一行创建异常:return mDb.insert(“user”,null,initialValues);

1 个解决方案

#1


39  

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.

另外,推荐的方法是将_ID列作为主键,即使您不使用它也是如此。这种方式在未来的使用中,如在适配器或列表中使用,您将不必解决方法。

#1


39  

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.

另外,推荐的方法是将_ID列作为主键,即使您不使用它也是如此。这种方式在未来的使用中,如在适配器或列表中使用,您将不必解决方法。