Doja
Doja

Reputation: 135

Old data in database keeps getting replaced by new data

Every time I insert new data into my database, a new row is added but the old rows of data gets replaced by the new one. I can't see any error in my code.

I'll paste below a snippet of my code.

private static class DBHelper extends SQLiteOpenHelper {



    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(DATABASE_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

// Open the database connection.
public DBAdapter open() {
    myDatabase = myDBHelper.getReadableDatabase();
    return this;
}

// Close the database connection.
public void close() {
    myDBHelper.close();
}

// Add a new set of values to the database.
public long insertRow(String name, String date, String time, String duration) {

    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_TIME, time);
    initialValues.put(KEY_DURATION, duration);

    // Insert it into the database.
    return myDatabase.insert(DATABASE_TABLE, null, initialValues);

}

...

edit

This is how my database is displayed:

 public void displayRecordSet(Cursor cursor) {

    ListView list = (ListView) findViewById(R.id.LIST);

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();

    if (cursor.moveToFirst()) {
        do {
            String _id = cursor.getString(DBAdapter.COL_ROWID);
            String date = cursor.getString(DBAdapter.COL_DATE);
            String time = cursor.getString(DBAdapter.COL_TIME);
            String duration = cursor.getString(DBAdapter.COL_DURATION);

                map.put("_id", _id);
                map.put("date", date);
                map.put("time", time);
                map.put("duration", duration);
                mylist.add(map);

           } while(cursor.moveToNext());

        SimpleAdapter myTable = new SimpleAdapter(this, mylist, R.layout.row,
        new String[] {"_id", "date", "time", "duration"}, new int[] {R.id.ID, R.id.DATE, R.id.TIME, R.id.DURATION});
        list.setAdapter(myTable);   
    }

    cursor.close();     
}

Upvotes: 0

Views: 593

Answers (2)

Gracker
Gracker

Reputation: 524

you can do like this:

if (cursor.moveToFirst()) {
    do {
        String _id = cursor.getString(DBAdapter.COL_ROWID);
        String date = cursor.getString(DBAdapter.COL_DATE);
        String time = cursor.getString(DBAdapter.COL_TIME);
        String duration = cursor.getString(DBAdapter.COL_DURATION);
        HashMap<String, String> map = new HashMap<String, String>();

        map.put("_id", _id);
        map.put("date", date);
        map.put("time", time);
        map.put("duration", duration);
        mylist.add(map);

       } while(cursor.moveToNext());

    SimpleAdapter myTable = new SimpleAdapter(this, mylist, R.layout.row,
    new String[] {"_id", "date", "time", "duration"}, new int[] {R.id.ID, R.id.DATE, R.id.TIME, R.id.DURATION});
    list.setAdapter(myTable);   
}

Upvotes: 1

laalto
laalto

Reputation: 152927

The problem is in displayRecordSet(). You're using the same map object for each row in the result set and rewrite values in the map over and over again and adding the same map again to the result list. As a result, the result list contains multiple copies of the same map with values from the last row.

To fix it, create a new object for each iteration in the do-while loop. For example, move the

HashMap<String, String> map = new HashMap<String, String>();

inside the loop.

Upvotes: 1

Related Questions