@@ -26,7 +26,7 @@ public class DBHelper
2626 private static final String TAG = DBHelper .class .getSimpleName ();
2727
2828 private final Database db ;
29-
29+
3030 private ArrayList <DbData > dbDataArrayList = new ArrayList <>();
3131 private ArrayList <DbColumn > dbColumnArrayList = new ArrayList <>();
3232
@@ -1036,6 +1036,152 @@ public DBHelper insertData(String tableName)
10361036
10371037 return this ;
10381038 }
1039+
1040+ //#region COMMENTS FOR insertDataWithTransaction method
1041+ /**
1042+ * 2019 January 09 - Wednesday - 06:49 PM
1043+ * insert data with transaction method
1044+ *
1045+ * @param tableName - name of the table where the data is to be inserted
1046+ *
1047+ * this method will insert data into table using database transaction
1048+ * this method is useful for inserting bulk records into table in less time
1049+ **/
1050+ //#endregion COMMENTS FOR insertDataWithTransaction method
1051+ @ Deprecated
1052+ public DBHelper insertDataWithTransaction (String tableName )
1053+ {
1054+ if (dbDataArrayList == null || dbDataArrayList .size () == 0 )
1055+ {
1056+ Log .e (TAG , "insertDataWithTransaction: Db Data was not provided. Cannot insert data in table." );
1057+ return this ;
1058+ }
1059+
1060+ // tree set is used for removing duplicate column name from data array list
1061+ TreeSet <String > treeSet = new TreeSet <>();
1062+
1063+ // this array list will hold unique column name from data array list
1064+ ArrayList <String > columnsArrayList = new ArrayList <>();
1065+
1066+ // loop for removing duplicate values from data array list
1067+ // for (int i = 0; i < dbDataArrayList.size(); i++)
1068+ for (int i = 0 ; i < dbDataArrayList .size (); i ++)
1069+ {
1070+ for (DbData item : dbDataArrayList )
1071+ {
1072+ // checking if tree set contains columns from data array list
1073+ // if not contains then adding it to columns array list
1074+ if (!treeSet .contains (item .columnName ))
1075+ {
1076+ // column name not present in columns array list
1077+ // adding to columns array list and tree set
1078+ treeSet .add (item .columnName );
1079+ columnsArrayList .add (item .columnName );
1080+ }
1081+ }
1082+ }
1083+
1084+ // getting columns count for generating insert query
1085+ // and inserting records into corresponding columns
1086+ int columnCount = columnsArrayList .size ();
1087+
1088+ // this string builder is used to append names of columns for the query
1089+ // for saving records into corresponding columns
1090+ StringBuilder queryBuilder = new StringBuilder ();
1091+
1092+ // this string builder is used to append indexes for the query
1093+ StringBuilder indexesBuilder = new StringBuilder ();
1094+
1095+ // generating insert query
1096+ queryBuilder .append ("INSERT INTO " ).append (tableName ).append (" (" );
1097+ indexesBuilder .append (" VALUES (" );
1098+
1099+ // loop for generating insert query with columns name and indexes
1100+ for (int i = 0 ; i < columnCount ; i ++)
1101+ {
1102+ indexesBuilder .append ("?" );
1103+ queryBuilder .append (columnsArrayList .get (i ));
1104+
1105+ // checking if column's count is equals to i
1106+ // if yes then appending brackets
1107+ // else appending comma
1108+ if (i == columnCount - 1 )
1109+ {
1110+ queryBuilder .append (")" );
1111+ indexesBuilder .append (")" );
1112+ }
1113+ else
1114+ {
1115+ queryBuilder .append (" , " );
1116+ indexesBuilder .append (" , " );
1117+ }
1118+ }
1119+
1120+ // this is final query
1121+ String query = queryBuilder .toString () + indexesBuilder .toString ();
1122+ Log .e (TAG , "insertDataWithTransaction: Insert query with transaction is: " + query );
1123+
1124+ // starting database transaction for inserting records
1125+ db .getWritableDatabase ().beginTransaction ();
1126+
1127+ // compiling insert query with indexes
1128+ SQLiteStatement statement = db .getWritableDatabase ().compileStatement (query );
1129+
1130+ // this position is used for SQLite statement
1131+ // for binding data with columns
1132+ int position = 0 ;
1133+
1134+ // loop for inserting records with statement
1135+ for (int i = 0 ; i <= dbDataArrayList .size (); i ++)
1136+ {
1137+ // checking if position is equals to column count
1138+ // this check will make sure that only those records get inserted
1139+ // for which the columns are passed
1140+ // irrespective of no of columns table has
1141+ // if yes then executing the statement
1142+ if (position == columnCount )
1143+ {
1144+ position = 0 ;
1145+ statement .execute ();
1146+ statement .clearBindings ();
1147+ }
1148+
1149+ // checking if i is equals to data array list's size
1150+ // if yes then breaking loop so the below code is not executed
1151+ // this check will ensure that last records are inserted
1152+ // and no index out of bound exception occurs
1153+ if (i == dbDataArrayList .size ())
1154+ {
1155+ continue ;
1156+ }
1157+
1158+ // increasing the position value by 1 for mapping data with column
1159+ position += 1 ;
1160+
1161+ // retrieving data from data array list
1162+ Object columnData = dbDataArrayList .get (i ).columnData ;
1163+
1164+ // checking the type of data and binding to corresponding type
1165+ if (columnData instanceof Integer )
1166+ {
1167+ statement .bindLong (position , Integer .parseInt (columnData .toString ()));
1168+ }
1169+ else if (columnData instanceof String )
1170+ {
1171+ statement .bindString (position , columnData .toString ());
1172+ }
1173+ else if (columnData instanceof Double || columnData instanceof Float )
1174+ {
1175+ statement .bindDouble (position , Double .parseDouble (columnData .toString ()));
1176+ }
1177+ }
1178+
1179+ db .getWritableDatabase ().setTransactionSuccessful ();
1180+ db .getWritableDatabase ().endTransaction ();
1181+
1182+ dbDataArrayList = new ArrayList <>();
1183+ return this ;
1184+ }
10391185
10401186 //#region COMMENTS FOR insertDataWithTransaction method
10411187 /**
@@ -1044,11 +1190,13 @@ public DBHelper insertData(String tableName)
10441190 *
10451191 * @param tableName - name of the table where the data is to be inserted
10461192 *
1193+ * @param dbColumnCount - total number of columns in the table you want to insert data
1194+ *
10471195 * this method will insert data into table using database transaction
10481196 * this method is useful for inserting bulk records into table in less time
10491197 **/
10501198 //#endregion COMMENTS FOR insertDataWithTransaction method
1051- public DBHelper insertDataWithTransaction (String tableName )
1199+ public DBHelper insertDataWithTransaction (String tableName , int dbColumnCount )
10521200 {
10531201 if (dbDataArrayList == null || dbDataArrayList .size () == 0 )
10541202 {
@@ -1063,10 +1211,16 @@ public DBHelper insertDataWithTransaction(String tableName)
10631211 ArrayList <String > columnsArrayList = new ArrayList <>();
10641212
10651213 // loop for removing duplicate values from data array list
1066- for (int i = 0 ; i < dbDataArrayList .size (); i ++)
1214+ // for (int i = 0; i < dbDataArrayList.size(); i++)
1215+ for (int i = 0 ; i < dbColumnCount ; i ++)
10671216 {
10681217 for (DbData item : dbDataArrayList )
10691218 {
1219+ if (columnsArrayList .size () == dbColumnCount )
1220+ {
1221+ break ;
1222+ }
1223+
10701224 // checking if tree set contains columns from data array list
10711225 // if not contains then adding it to columns array list
10721226 if (!treeSet .contains (item .columnName ))
0 commit comments