数据库中的旧数据不断被新数据取代

时间:2014-03-17 21:47:06

标签: java android sql database sqlite

每次我将新数据插入到我的数据库中时,都会添加一个新行,但旧行数据会被新行替换。我在代码中看不到任何错误。

我会在我的代码片段下方粘贴。

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

}

...

修改

这是我的数据库显示方式:

 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();     
}

2 个答案:

答案 0 :(得分:1)

问题出在displayRecordSet()。您为结果集中的每一行使用相同的map对象,并一遍又一遍地重写map中的值,并将相同的地图再次添加到结果列表中。因此,结果列表包含同一个映射的多个副本,其中包含最后一行的值。

要修复它,请为do - while循环中的每次迭代创建一个新对象。例如,移动

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

在循环中。

答案 1 :(得分:1)

你可以这样做:

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