Android - SQLite删除和更新数据不能正常工作

时间:2013-11-11 15:14:08

标签: android sqlite arraylist

我创建了几个表的数据库,得到了一个名为friend的表。朋友花费很少,一些费用可能与另一位朋友分享。现在我想删除一个朋友,我想要做的是当朋友与另一个朋友分享费用时,与另一个朋友共享的朋友的费用将被添加到另一个朋友并且费用将不会被删除。然后,直接删除未与其他朋友共享的费用,并且由于朋友有很多费用,因此不会将其添加到任何朋友。现在问题是当我试图将费用添加到另一个朋友时,它不起作用。我没有收到任何错误。

这是我的代码:我认为问题发生在if(sharerList.size()> 1)....,因为其余的代码运行良好。

  public void deleteFriend(String id) {
    Log.d(LOGCAT, "delete");
    SQLiteDatabase database = this.getWritableDatabase();
    ArrayList<HashMap<String, String>> wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM FriendsExpenses WHERE friendId='" + id + "'";
    SQLiteDatabase databaseread = this.getReadableDatabase();
    Cursor cursor = databaseread.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("expenseId", cursor.getString(0));
            wordList.add(map);
        } while (cursor.moveToNext());
    }
    for (int a=0; a<wordList.size();a++){
        HashMap<String, String> ValexpenseId = wordList.get(a);
        for (Entry<String, String> entry : ValexpenseId.entrySet()) {
            String value = entry.getValue();
            String selectQuery2 = "SELECT * FROM FriendsExpenses WHERE expenseId='" + value + "'";
            SQLiteDatabase databaseread2 = this.getReadableDatabase();
            Cursor cursor2 = databaseread2.rawQuery(selectQuery2, null);
            ArrayList<HashMap<String, String>> sharerList = new ArrayList<HashMap<String, String>>();
            if (cursor2.moveToFirst()) {
                do {
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("friendId", cursor2.getString(0));
                    wordList.add(map);
                } while (cursor2.moveToNext());
            }
            else{};
            if (sharerList.size() > 1){
                String selectQuery3 = "SELECT expenseTotal FROM expenses WHERE expenseId='" + value + "'";
                SQLiteDatabase databaseread3 = this.getReadableDatabase();
                Cursor cursor3 = databaseread3.rawQuery(selectQuery3, null);
                String expenseTotal = null;
                if (cursor3.moveToFirst()) {
                    do {
                        expenseTotal = cursor3.getString(cursor3.getColumnIndex("expenseTotal"));
                    } while (cursor3.moveToNext());
                }
                for (int b=0; b<sharerList.size();b++){
                    HashMap<String, String> ValfriendId = sharerList.get(b);
                    for (Entry<String, String> entry2 : ValfriendId.entrySet()) {
                        String value2 = entry2.getValue();
                        String currentSpend = currentSpending(value2);
                        double currentSpending = (Double.parseDouble(currentSpend));
                        double expTotal = (Double.parseDouble(expenseTotal));
                        double newSpending = currentSpending + ((expTotal/sharerList.size()) /sharerList.size()-1);
                        updateSpending(value2, newSpending);
                    }
                }
            }
            else
            {
                String deleteQuery = "DELETE FROM expenses where expenseId='" + value + "'";
                Log.d("query", deleteQuery);
                database.execSQL(deleteQuery);
            }               
        }
    }
    String deleteQuery = "DELETE FROM friends where friendId='" + id + "'";
    String deleteQuery2 = "DELETE FROM FriendsExpenses where friendId='" + id + "'";
    Log.d("query", deleteQuery2);
    Log.d("query", deleteQuery);
    database.execSQL(deleteQuery2);
    database.execSQL(deleteQuery);
}

1 个答案:

答案 0 :(得分:0)

sharerList为空,因为您从不添加任何条目。

可能存在其他复制/粘贴错误,例如getString(0)SELECT *