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

时间:2014-01-08 06:25:10

标签: android sqlite error-code

我知道你在这里有很多关于这个错误的问题,但是我无法理解为什么所有字段似乎都在Logcat上正确填充,但即便如此,也会发生故障。

这是表的创建:

public void onCreate(SQLiteDatabase db) {
        String ddl = "CREATE TABLE Politician (id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE , idpolitician INTEGER NOT NULL, name TEXT NOT NULL, picture TEXT , position TEXT NOT NULL, country TEXT NOT NULL, state TEXT NOT NULL, city TEXT NOT NULL);";

        db.execSQL(ddl);

    }

这是我试图保存的地方。

public void savePolitician(Politician politician) {

        ContentValues values = new ContentValues();
        values.put("idpolitician", politician.getId());
        values.put("name", politician.getName());
        values.put("picture", politician.getPictureFilename());
        values.put("position", politician.getPosition());
        values.put("country", politician.getCountry());
        values.put("state", politician.getState());
        values.put("city", politician.getCity());

        mDatabase.insert("Politician", null, values);

    }

这是LogCat:

01-08 04:03:31.149  32633-32633/? E/Database﹕ [SQLiteDatabase.java:1428:insert()] Error inserting 

state=SP position=governador picture=example idpolitician=1 city=Sao Paulo country=Brasil name=Alckmin

    android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
            at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
            at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
            at com.politify.dao.DbManegament.savePolitician(DbManegament.java:44)

正如您所看到的,所有字段都已填满。任何人都可以帮助并告诉我我做错了什么

2 个答案:

答案 0 :(得分:0)

尝试删除您的创建表脚本中的AUTOINCREMENT UNIQUE

答案 1 :(得分:0)

这是你的错误id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE

当您将任何列定义为主键时,您无需将其指定为UNIQUE。主键默认为UNIQUE。