title: Android - SQLite delete and update data not working well
body: <p>I created database with few tables, got 1 table called friend. A friend has few expenses, some expenses might share with another friend. Now i am trying to delete a friend, what i am trying to do is when the friend share expenses with another friend, the expense of the friend that shared with another friend will then added to another friend and the expense will not deleted. Then, the expenses that are not shared with another friend is directly deleted and will not added to any friend since the friend has many expenses. Now the problem is when i trying to add the expense to another friend, its not working. And i am not getting any error.</p>
  
  <p>Here is my code : i think the problem occur inside if(sharerList.size()>1)...., since the rest of the code works well.</p>
  
  <pre><code>  public void deleteFriend(String id) {
      Log.d(LOGCAT, "delete");
      SQLiteDatabase database = this.getWritableDatabase();
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; wordList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
      String selectQuery = "SELECT * FROM FriendsExpenses WHERE friendId='" + id + "'";
      SQLiteDatabase databaseread = this.getReadableDatabase();
      Cursor cursor = databaseread.rawQuery(selectQuery, null);
      if (cursor.moveToFirst()) {
          do {
              HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
              map.put("expenseId", cursor.getString(0));
              wordList.add(map);
          } while (cursor.moveToNext());
      }
      for (int a=0; a&lt;wordList.size();a++){
          HashMap&lt;String, String&gt; ValexpenseId = wordList.get(a);
          for (Entry&lt;String, String&gt; 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&lt;HashMap&lt;String, String&gt;&gt; sharerList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
              if (cursor2.moveToFirst()) {
                  do {
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                      map.put("friendId", cursor2.getString(0));
                      wordList.add(map);
                  } while (cursor2.moveToNext());
              }
              else{};
              if (sharerList.size() &gt; 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&lt;sharerList.size();b++){
                      HashMap&lt;String, String&gt; ValfriendId = sharerList.get(b);
                      for (Entry&lt;String, String&gt; 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);
  }
  </code></pre>