0

I'm inserting data in table, when db is created first time the data is inserted successfully, but when app is closed and re-launched it gives me error:

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)

This is the code I'm using in my 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 Code for Inserting data into db is :

// 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 Create Table Query is :

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 );";

now I'm unable to find why is this error occurring. Thanks

6
  • have a look at this link stackoverflow.com/questions/8117685/… Commented Mar 5, 2013 at 6:14
  • 1
    read this too stackoverflow.com/questions/7567362/… and this too stackoverflow.com/questions/4499201/… Commented Mar 5, 2013 at 6:15
  • show the code where you inserting the data into the database. Commented Mar 5, 2013 at 6:17
  • Also post your create table query with all the constraints specified.. May be there is issue of primary key constraint, some records with the same IDs are getting inserted, throwing an error. Make sure the records are not getting added multiple times as you re-run the app. Commented Mar 5, 2013 at 6:21
  • 1
    In your table creation "MOBILE_CONTENT_ID" is INTEGER PRIMARY KEY AUTOINCREMENT,so in insert query don't put value to "MOBILE_CONTENT_ID". Commented Mar 5, 2013 at 6:38

2 Answers 2

3

In your table creation "MOBILE_CONTENT_ID" is INTEGER PRIMARY KEY AUTOINCREMENT,So in insert query don't put value to "MOBILE_CONTENT_ID".

Sign up to request clarification or add additional context in comments.

Comments

1

you got this error because your table has field MOBILE_CONTENT_ID has primary key and is auto incremented.and you are altering the primary key.you have 2 options 1. Remove primary key for that field 2. Don't insert value in auto incremented field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.