Skip to content

Commit

Permalink
1. Resolved a major bug where data was not updating for a table.
Browse files Browse the repository at this point in the history
  • Loading branch information
amitjangid80 committed Nov 21, 2019
1 parent 4319d12 commit 9124e73
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion app/src/main/java/com/amit/db/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,84 @@ else if (object instanceof JSONArray)
}
}

//#region COMMENTS FOR updateData method
/**
* 2019 January 08 - Tuesday - 04:28 PM
* update data method
*
* @param tableName - name of the table on which update query is to be performed
*
* @param whereClause - name of the column to check whether the record is present so the data is updated
* pass this parameter in the way given in example below
* Ex: code = ? or ID = ? etc // this is important
*
* this method will update records of the table in database
* this method uses database's update method for updating records
*
* parameter whereClause and whereArgs must be passed in the form given
**/
//#endregion COMMENTS FOR updateData method
public DBHelper updateData(String tableName, String whereClause)
{
try
{
// checking if table name was provided or not
if (tableName == null || tableName.isEmpty())
{
if (dbDataArrayList != null)
{
dbDataArrayList.clear();
}

Log.e(TAG, "updateData: Table name was null or empty.");
return this;
}

// checking if column name was provided or not
if (whereClause == null || whereClause.isEmpty())
{
if (dbDataArrayList != null)
{
dbDataArrayList.clear();
}

Log.e(TAG, "updateData: Column name was null or empty.");
return this;
}

// checking if data was provided or not
if (dbDataArrayList == null || dbDataArrayList.size() == 0)
{
Log.e(TAG, "updateData: Data was not provided for updating records.");
return this;
}

// content values for putting column name
// and data for inserting into database table
ContentValues contentValues = new ContentValues();

// loop for no of data provided
for (int i = 0; i < dbDataArrayList.size(); i++)
{
// adding column names and column data into content values
contentValues.put(dbDataArrayList.get(i).columnName, dbDataArrayList.get(i).columnData.toString());
}

// you can directly pass the values to where clause
db.getWritableDatabase().update(tableName, contentValues, whereClause, null);
dbDataArrayList = new ArrayList<>();
}
catch (Exception e)
{
Log.e(TAG, "updateData: exception while updating records in table: " + tableName + ":\n");
e.printStackTrace();

dbDataArrayList = new ArrayList<>();
}

return this;
}

//#region COMMENTS FOR updateData method
/**
* 2019 January 08 - Tuesday - 04:28 PM
Expand Down Expand Up @@ -1849,7 +1927,7 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
}

// checking if column data was provided or not
if (whereArgs != null && whereArgs.isEmpty())
if (whereArgs != null && !whereArgs.isEmpty())
{
db.getWritableDatabase().update(tableName, contentValues, whereClause, new String[]{whereArgs});
}
Expand Down

0 comments on commit 9124e73

Please sign in to comment.