Skip to content

Commit 9124e73

Browse files
committed
1. Resolved a major bug where data was not updating for a table.
1 parent 4319d12 commit 9124e73

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

app/src/main/java/com/amit/db/DBHelper.java

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,84 @@ else if (object instanceof JSONArray)
17811781
}
17821782
}
17831783

1784+
//#region COMMENTS FOR updateData method
1785+
/**
1786+
* 2019 January 08 - Tuesday - 04:28 PM
1787+
* update data method
1788+
*
1789+
* @param tableName - name of the table on which update query is to be performed
1790+
*
1791+
* @param whereClause - name of the column to check whether the record is present so the data is updated
1792+
* pass this parameter in the way given in example below
1793+
* Ex: code = ? or ID = ? etc // this is important
1794+
*
1795+
* this method will update records of the table in database
1796+
* this method uses database's update method for updating records
1797+
*
1798+
* parameter whereClause and whereArgs must be passed in the form given
1799+
**/
1800+
//#endregion COMMENTS FOR updateData method
1801+
public DBHelper updateData(String tableName, String whereClause)
1802+
{
1803+
try
1804+
{
1805+
// checking if table name was provided or not
1806+
if (tableName == null || tableName.isEmpty())
1807+
{
1808+
if (dbDataArrayList != null)
1809+
{
1810+
dbDataArrayList.clear();
1811+
}
1812+
1813+
Log.e(TAG, "updateData: Table name was null or empty.");
1814+
return this;
1815+
}
1816+
1817+
// checking if column name was provided or not
1818+
if (whereClause == null || whereClause.isEmpty())
1819+
{
1820+
if (dbDataArrayList != null)
1821+
{
1822+
dbDataArrayList.clear();
1823+
}
1824+
1825+
Log.e(TAG, "updateData: Column name was null or empty.");
1826+
return this;
1827+
}
1828+
1829+
// checking if data was provided or not
1830+
if (dbDataArrayList == null || dbDataArrayList.size() == 0)
1831+
{
1832+
Log.e(TAG, "updateData: Data was not provided for updating records.");
1833+
return this;
1834+
}
1835+
1836+
// content values for putting column name
1837+
// and data for inserting into database table
1838+
ContentValues contentValues = new ContentValues();
1839+
1840+
// loop for no of data provided
1841+
for (int i = 0; i < dbDataArrayList.size(); i++)
1842+
{
1843+
// adding column names and column data into content values
1844+
contentValues.put(dbDataArrayList.get(i).columnName, dbDataArrayList.get(i).columnData.toString());
1845+
}
1846+
1847+
// you can directly pass the values to where clause
1848+
db.getWritableDatabase().update(tableName, contentValues, whereClause, null);
1849+
dbDataArrayList = new ArrayList<>();
1850+
}
1851+
catch (Exception e)
1852+
{
1853+
Log.e(TAG, "updateData: exception while updating records in table: " + tableName + ":\n");
1854+
e.printStackTrace();
1855+
1856+
dbDataArrayList = new ArrayList<>();
1857+
}
1858+
1859+
return this;
1860+
}
1861+
17841862
//#region COMMENTS FOR updateData method
17851863
/**
17861864
* 2019 January 08 - Tuesday - 04:28 PM
@@ -1849,7 +1927,7 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
18491927
}
18501928

18511929
// checking if column data was provided or not
1852-
if (whereArgs != null && whereArgs.isEmpty())
1930+
if (whereArgs != null && !whereArgs.isEmpty())
18531931
{
18541932
db.getWritableDatabase().update(tableName, contentValues, whereClause, new String[]{whereArgs});
18551933
}

0 commit comments

Comments
 (0)