@@ -1113,6 +1113,58 @@ public DBHelper insertData(String tableName)
1113
1113
return this ;
1114
1114
}
1115
1115
1116
+ //#region COMMENTS FOR insertData method
1117
+ /**
1118
+ * 2019 January 08 - Tuesday - 04:03 PM
1119
+ * insert data with return id method
1120
+ *
1121
+ * this method is used for inserting records into table
1122
+ *
1123
+ * @param tableName - name of the table for inserting records
1124
+ *
1125
+ * @return inserted row id if successful or -1 if not inserted
1126
+ **/
1127
+ //#endregion COMMENTS FOR insertData method
1128
+ public long insertDataWithReturnId (String tableName )
1129
+ {
1130
+ // checking if table name was provided or not
1131
+ if (tableName == null || tableName .isEmpty ())
1132
+ {
1133
+ if (dbDataArrayList != null )
1134
+ {
1135
+ dbDataArrayList .clear ();
1136
+ }
1137
+
1138
+ Log .e (TAG , "insertData: Table name was null or empty." );
1139
+ return -1 ;
1140
+ }
1141
+
1142
+ // checking if data was provided or not for inserting in database
1143
+ if (dbDataArrayList == null || dbDataArrayList .size () == 0 )
1144
+ {
1145
+ Log .e (TAG , "insertData: No data provided for inserting." );
1146
+ return -1 ;
1147
+ }
1148
+
1149
+ // content values for putting column name
1150
+ // and data for inserting into database table
1151
+ ContentValues contentValues = new ContentValues ();
1152
+
1153
+ // loop for no of data to be inserted
1154
+ for (int i = 0 ; i < dbDataArrayList .size (); i ++)
1155
+ {
1156
+ // adding column names and column data into content values
1157
+ contentValues .put (dbDataArrayList .get (i ).columnName , dbDataArrayList .get (i ).columnData .toString ());
1158
+ }
1159
+
1160
+ // executing inserting statement for inserting records in table
1161
+ long insertedId = db .getWritableDatabase ().insert (tableName , null , contentValues );
1162
+ db .close ();
1163
+
1164
+ dbDataArrayList = new ArrayList <>();
1165
+ return insertedId ;
1166
+ }
1167
+
1116
1168
//#region COMMENTS FOR insertDataWithTransaction method
1117
1169
/**
1118
1170
* 2019 January 09 - Wednesday - 06:49 PM
@@ -1470,9 +1522,6 @@ else if (object instanceof JSONArray)
1470
1522
String columnName = iterator .next ();
1471
1523
String columnData = jsonObject .getString (columnName );
1472
1524
1473
- // Log.e(TAG, "insertData: name of column from json is: " + columnName);
1474
- // Log.e(TAG, "insertData: value of column from json is: " + columnData);
1475
-
1476
1525
this .addDataForTable (new DbData (columnName , columnData ));
1477
1526
}
1478
1527
}
@@ -1489,6 +1538,80 @@ else if (object instanceof JSONArray)
1489
1538
}
1490
1539
}
1491
1540
1541
+ //#region COMMENTS FOR insertDataWithJson method
1542
+ /**
1543
+ * 2019 Apr 25 - Thursday - 12:25 PM
1544
+ * insert data with json with return id method
1545
+ *
1546
+ * this method will insert data using JSON Array or JSON Object
1547
+ *
1548
+ * @param tableName - name of the table to insert data in
1549
+ *
1550
+ * @param object - JSON Object or JSON Array of records and columns to be inserted
1551
+ *
1552
+ * @return True or False for success for failure in inserting records
1553
+ **/
1554
+ //#endregion COMMENTS FOR insertDataWithJson method
1555
+ public long insertDataWithJsonWithReturnId (String tableName , Object object )
1556
+ {
1557
+ try
1558
+ {
1559
+ JSONArray jsonArray = new JSONArray ();
1560
+
1561
+ if (object == null )
1562
+ {
1563
+ Log .e (TAG , "insertData: object value cannot be null." );
1564
+ return -1 ;
1565
+ }
1566
+
1567
+ if (object instanceof ArrayList )
1568
+ {
1569
+ Log .e (TAG , "insertDataWithJson: cannot parse array list, you can use json object or json array." );
1570
+ return -1 ;
1571
+ }
1572
+
1573
+ if (object instanceof JSONObject )
1574
+ {
1575
+ Iterator <String > iterator = ((JSONObject ) object ).keys ();
1576
+
1577
+ while (iterator .hasNext ())
1578
+ {
1579
+ String key = iterator .next ();
1580
+ jsonArray = ((JSONObject ) object ).getJSONArray (key );
1581
+
1582
+ Log .e (TAG , "insertData: json array for " + key + " is: " + jsonArray );
1583
+ }
1584
+ }
1585
+ else if (object instanceof JSONArray )
1586
+ {
1587
+ jsonArray = (JSONArray ) object ;
1588
+ }
1589
+
1590
+ for (int i = 0 ; i < jsonArray .length (); i ++)
1591
+ {
1592
+ JSONObject jsonObject = jsonArray .getJSONObject (i );
1593
+ Iterator <String > iterator = jsonObject .keys ();
1594
+
1595
+ while (iterator .hasNext ())
1596
+ {
1597
+ String columnName = iterator .next ();
1598
+ String columnData = jsonObject .getString (columnName );
1599
+
1600
+ this .addDataForTable (new DbData (columnName , columnData ));
1601
+ }
1602
+ }
1603
+
1604
+ return this .insertDataWithReturnId (tableName );
1605
+ }
1606
+ catch (Exception e )
1607
+ {
1608
+ Log .e (TAG , "insertData: exception while inserting data using json:\n " );
1609
+ e .printStackTrace ();
1610
+
1611
+ return -1 ;
1612
+ }
1613
+ }
1614
+
1492
1615
//#region COMMENTS FOR insertDataWithJsonAndTransaction method
1493
1616
/**
1494
1617
* 2019 Apr 25 - Thursday - 12:25 PM
@@ -1641,6 +1764,92 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
1641
1764
1642
1765
return this ;
1643
1766
}
1767
+
1768
+ //#region COMMENTS FOR updateData method
1769
+ /**
1770
+ * 2019 January 08 - Tuesday - 04:28 PM
1771
+ * update data with return id method
1772
+ *
1773
+ * @param tableName - name of the table on which update query is to be performed
1774
+ *
1775
+ * @param whereClause - name of the column to check whether the record is present so the data is updated
1776
+ * pass this parameter in the way given in example below
1777
+ * Ex: code = ? or ID = ? etc // this is important
1778
+ *
1779
+ * @param whereArgs - data of the column name provided to check if record is present for data update
1780
+ * here you need to pass the data for the corresponding where clause
1781
+ * Ex: 1 or 2 etc
1782
+ *
1783
+ * this method will update records of the table in database
1784
+ * this method uses database's update method for updating records
1785
+ *
1786
+ * parameter whereClause and whereArgs must be passed in the form given
1787
+ *
1788
+ * @return -1 if failed to update the record
1789
+ **/
1790
+ //#endregion COMMENTS FOR updateData method
1791
+ public long updateDataWithReturnId (String tableName , String whereClause , String whereArgs )
1792
+ {
1793
+ // checking if table name was provided or not
1794
+ if (tableName == null || tableName .isEmpty ())
1795
+ {
1796
+ if (dbDataArrayList != null )
1797
+ {
1798
+ dbDataArrayList .clear ();
1799
+ }
1800
+
1801
+ Log .e (TAG , "updateData: Table name was null or empty." );
1802
+ return -1 ;
1803
+ }
1804
+
1805
+ // checking if column name was provided or not
1806
+ if (whereClause == null || whereClause .isEmpty ())
1807
+ {
1808
+ if (dbDataArrayList != null )
1809
+ {
1810
+ dbDataArrayList .clear ();
1811
+ }
1812
+
1813
+ Log .e (TAG , "updateData: Column name was null or empty." );
1814
+ return -1 ;
1815
+ }
1816
+
1817
+ // checking if data was provided or not
1818
+ if (dbDataArrayList == null || dbDataArrayList .size () == 0 )
1819
+ {
1820
+ Log .e (TAG , "updateData: Data was not provided for updating records." );
1821
+ return -1 ;
1822
+ }
1823
+
1824
+ // content values for putting column name
1825
+ // and data for inserting into database table
1826
+ ContentValues contentValues = new ContentValues ();
1827
+
1828
+ // loop for no of data provided
1829
+ for (int i = 0 ; i < dbDataArrayList .size (); i ++)
1830
+ {
1831
+ // adding column names and column data into content values
1832
+ contentValues .put (dbDataArrayList .get (i ).columnName , dbDataArrayList .get (i ).columnData .toString ());
1833
+ }
1834
+
1835
+ long updatedId ;
1836
+
1837
+ // checking if column data was provided or not
1838
+ if (whereArgs != null && whereArgs .isEmpty ())
1839
+ {
1840
+ updatedId = db .getWritableDatabase ().update (tableName , contentValues , whereClause , new String []{whereArgs });
1841
+ }
1842
+ else
1843
+ {
1844
+ // you can directly pass the values to where clause
1845
+ updatedId = db .getWritableDatabase ().update (tableName , contentValues , whereClause , null );
1846
+ }
1847
+
1848
+ db .close ();
1849
+ dbDataArrayList = new ArrayList <>();
1850
+
1851
+ return updatedId ;
1852
+ }
1644
1853
1645
1854
//#region COMMENTS FOR deleteTable method
1646
1855
/**
0 commit comments