0

我在表中插入数据,当第一次创建数据库时成功插入数据,但是当应用程序关闭并重新启动时,它给了我错误:

Error inserting id=1 new=1 title=sample page previous=0 color=0 next=2 state=1 header= "";
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.testjsonparse.MainActivity.parseAndIsertData(MainActivity.java:108)
at java.lang.reflect.Method.invokeNative(Native Method)

这是我在我的MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     

    dataSource = new ContentsDataSource(this);
    dataSource.open();
    parseAndIsertData();

}


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    dataSource.open();

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.i(MY_TAGT, "OnPuse() called");      
    dataSource.close();
}
private void parseAndIsertData() {
    // Creating JSON Parser instance
    MyJSONParser jParser = new MyJSONParser();

    contentDataObject = new ContentDataObject();        
    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(BASE_URL); 

    try {
        // Getting Array of Contents
        jsonArray = json.getJSONArray(MOBILE_CONTENT);          
        // looping through All Contents
        for(int i = 0; i < jsonArray.length(); i++){

            contentDataObject.setId(jsonArray.getJSONObject(i).getInt(MOBILE_CONTENT_ID));
            contentDataObject.setTitle(jsonArray.getJSONObject(i).getString(MOBILE_CONTENT_TITLE));
            contentDataObject.setFulltext(jsonArray.getJSONObject(i).getString(MOBILE_CONTENT_FULLTEXT));
            contentDataObject.setState(jsonArray.getJSONObject(i).getInt(MOBILE_CONTENT_STATE));
            contentDataObject.setNewValue(jsonArray.getJSONObject(i).getInt(MOBILE_CONTENT_NEW));
            contentDataObject.setHeader(jsonArray.getJSONObject(i).getString(MOBILE_CONTENT_HEADER));
            contentDataObject.setColor(jsonArray.getJSONObject(i).getInt(MOBILE_CONTENT_COLOR));
            contentDataObject.setNext(jsonArray.getJSONObject(i).getString(MOBILE_CONTENT_NEXT));
            contentDataObject.setPrevious(jsonArray.getJSONObject(i).getString(MOBILE_CONTENT_PREVIOUS));

            contentDataObject = dataSource.create(contentDataObject);

            Log.i(MY_TAGT, "Data Inserted " + contentDataObject.getId() + " Times");

        }           

        //textView.setText(builder);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

EDIT插入数据的代码db是:

// Insert into Database

    public ContentDataObject create( ContentDataObject dataObject) {
        // Content class
        ContentValues values = new ContentValues();

        values.put(DBHelper.MOBILE_CONTENT_ID, dataObject.getId());
        values.put(DBHelper.MOBILE_CONTENT_TITLE, dataObject.getTitle());
        values.put(DBHelper.MOBILE_CONTENT_FULLTEXT, dataObject.getFulltext());
        values.put(DBHelper.MOBILE_CONTENT_STATE, dataObject.getState());
        values.put(DBHelper.MOBILE_CONTENT_NEW, dataObject.getNewValue());
        values.put(DBHelper.MOBILE_CONTENT_HEADER, dataObject.getHeader());
        values.put(DBHelper.MOBILE_CONTENT_COLOR, dataObject.getColor());
        values.put(DBHelper.MOBILE_CONTENT_NEXT, dataObject.getNext());
        values.put(DBHelper.MOBILE_CONTENT_PREVIOUS, dataObject.getPrevious());

        long insetId = database.insert(DBHelper.MOBILE_CONTENT_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE);// Here it gives me error for SQLiteDatabase.CONFLICT_IGNORE
        dataObject.setId(insetId);
        return dataObject;

    }

EDIT创建表查询是:

private static final String MOBILE_CONTENT_DB_CREATE = "CREATE TABLE "
        + MOBILE_CONTENT_TABLE_NAME + "( " + MOBILE_CONTENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        MOBILE_CONTENT_TITLE    + " TEXT, " +
        MOBILE_CONTENT_FULLTEXT + " TEXT , " +
        MOBILE_CONTENT_STATE    + " INTEGER , " +
        MOBILE_CONTENT_NEW  + " INTEGER, " +
        MOBILE_CONTENT_HEADER   + " TEXT , " +
        MOBILE_CONTENT_COLOR    + " INTEGER , " +
        MOBILE_CONTENT_NEXT + " TEXT , " +
        MOBILE_CONTENT_PREVIOUS + " TEXT );";

现在我无法找到为什么会发生此错误。谢谢

4

2 回答 2

3

在您的表创建中“MOBILE_CONTENT_ID”是 INTEGER PRIMARY KEY AUTOINCREMENT,因此在插入查询中不要将值放入“MOBILE_CONTENT_ID”。

于 2013-03-05T06:45:29.740 回答
1

您收到此错误是因为您的表具有字段 MOBILE_CONTENT_ID 具有主键并且是自动递增的。并且您正在更改主键。您有 2 个选项 1. 删除该字段的主键 2. 不要在自动递增字段中插入值。

于 2013-03-05T06:45:48.703 回答