@@ -326,6 +326,8 @@ public boolean executeDatabaseOperations(String tableName, String operations,
326
326
// if successful then it will return true
327
327
// return true;
328
328
db .getWritableDatabase ().execSQL (query );
329
+ db .close ();
330
+
329
331
return true ;
330
332
}
331
333
catch (Exception e )
@@ -353,7 +355,7 @@ public Cursor executeSelectQuery(String query)
353
355
try
354
356
{
355
357
// query execution
356
- Cursor cursor = db .getWritableDatabase ().rawQuery (query , null );
358
+ Cursor cursor = db .getReadableDatabase ().rawQuery (query , null );
357
359
358
360
// if cursor is not null then moving the position to first
359
361
// and returning the cursor
@@ -448,7 +450,7 @@ public Cursor executeSelectQuery(String tableName, String values,
448
450
}
449
451
450
452
// executing query
451
- cursor = db .getWritableDatabase ().rawQuery (query , null );
453
+ cursor = db .getReadableDatabase ().rawQuery (query , null );
452
454
453
455
// if cursor is not null then moving the position to first
454
456
// and returning the cursor
@@ -551,7 +553,7 @@ public <T> ArrayList<T> executeSelectQuery(String tableName, String values,
551
553
}
552
554
553
555
// executing query
554
- cursor = db .getWritableDatabase ().rawQuery (query , null );
556
+ cursor = db .getReadableDatabase ().rawQuery (query , null );
555
557
556
558
// if cursor is not null then moving the position to first
557
559
// and returning the cursor
@@ -873,6 +875,8 @@ public boolean executeQuery(String query)
873
875
if (query != null && !query .equalsIgnoreCase ("" ))
874
876
{
875
877
db .getWritableDatabase ().execSQL (query );
878
+ db .close ();
879
+
876
880
return true ;
877
881
}
878
882
else
@@ -954,6 +958,11 @@ public DBHelper createTable(String tableName)
954
958
// checking if table name was provided or not
955
959
if (tableName == null || tableName .isEmpty ())
956
960
{
961
+ if (dbColumnArrayList != null )
962
+ {
963
+ dbColumnArrayList .clear ();
964
+ }
965
+
957
966
Log .e (TAG , "createTable: Table name was null or empty." );
958
967
return this ;
959
968
}
@@ -992,6 +1001,8 @@ public DBHelper createTable(String tableName)
992
1001
Log .e (TAG , "createTable: Create table query is: " + query .toString ());
993
1002
994
1003
db .getWritableDatabase ().execSQL (query .toString ());
1004
+ db .close ();
1005
+
995
1006
dbColumnArrayList = new ArrayList <>();
996
1007
997
1008
return this ;
@@ -1031,7 +1042,9 @@ public DBHelper alterTable(String tableName)
1031
1042
{
1032
1043
query = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + columnDataType ;
1033
1044
Log .e (TAG , "alterTable: query for adding new column or altering table is: " + query );
1045
+
1034
1046
db .getWritableDatabase ().execSQL (query );
1047
+ db .close ();
1035
1048
}
1036
1049
else
1037
1050
{
@@ -1065,6 +1078,11 @@ public DBHelper insertData(String tableName)
1065
1078
// checking if table name was provided or not
1066
1079
if (tableName == null || tableName .isEmpty ())
1067
1080
{
1081
+ if (dbDataArrayList != null )
1082
+ {
1083
+ dbDataArrayList .clear ();
1084
+ }
1085
+
1068
1086
Log .e (TAG , "insertData: Table name was null or empty." );
1069
1087
return this ;
1070
1088
}
@@ -1089,8 +1107,9 @@ public DBHelper insertData(String tableName)
1089
1107
1090
1108
// executing inserting statement for inserting records in table
1091
1109
db .getWritableDatabase ().insert (tableName , null , contentValues );
1110
+ db .close ();
1111
+
1092
1112
dbDataArrayList = new ArrayList <>();
1093
-
1094
1113
return this ;
1095
1114
}
1096
1115
@@ -1103,7 +1122,7 @@ public DBHelper insertData(String tableName)
1103
1122
*
1104
1123
* this method will insert data into table using database transaction
1105
1124
* this method is useful for inserting bulk records into table in less time
1106
- **/
1125
+ **/
1107
1126
//#endregion COMMENTS FOR insertDataWithTransaction method
1108
1127
@ Deprecated
1109
1128
public void insertDataWithTransaction (String tableName )
@@ -1235,6 +1254,7 @@ else if (columnData instanceof Double || columnData instanceof Float)
1235
1254
1236
1255
db .getWritableDatabase ().setTransactionSuccessful ();
1237
1256
db .getWritableDatabase ().endTransaction ();
1257
+ db .close ();
1238
1258
1239
1259
dbDataArrayList = new ArrayList <>();
1240
1260
}
@@ -1386,6 +1406,7 @@ else if (columnData instanceof Double || columnData instanceof Float)
1386
1406
1387
1407
db .getWritableDatabase ().setTransactionSuccessful ();
1388
1408
db .getWritableDatabase ().endTransaction ();
1409
+ db .close ();
1389
1410
1390
1411
dbDataArrayList = new ArrayList <>();
1391
1412
}
@@ -1456,7 +1477,7 @@ else if (object instanceof JSONArray)
1456
1477
}
1457
1478
}
1458
1479
1459
- this .insertDataWithTransaction (tableName , 5 );
1480
+ this .insertData (tableName );
1460
1481
return true ;
1461
1482
}
1462
1483
catch (Exception e )
@@ -1565,24 +1586,27 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
1565
1586
// checking if table name was provided or not
1566
1587
if (tableName == null || tableName .isEmpty ())
1567
1588
{
1589
+ if (dbDataArrayList != null )
1590
+ {
1591
+ dbDataArrayList .clear ();
1592
+ }
1593
+
1568
1594
Log .e (TAG , "updateData: Table name was null or empty." );
1569
1595
return this ;
1570
1596
}
1571
1597
1572
1598
// checking if column name was provided or not
1573
1599
if (whereClause == null || whereClause .isEmpty ())
1574
1600
{
1601
+ if (dbDataArrayList != null )
1602
+ {
1603
+ dbDataArrayList .clear ();
1604
+ }
1605
+
1575
1606
Log .e (TAG , "updateData: Column name was null or empty." );
1576
1607
return this ;
1577
1608
}
1578
1609
1579
- // checking if column data was provided or not
1580
- if (whereArgs == null || whereArgs .isEmpty ())
1581
- {
1582
- Log .e (TAG , "updateData: Column data was null or empty." );
1583
- return this ;
1584
- }
1585
-
1586
1610
// checking if data was provided or not
1587
1611
if (dbDataArrayList == null || dbDataArrayList .size () == 0 )
1588
1612
{
@@ -1600,8 +1624,19 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
1600
1624
// adding column names and column data into content values
1601
1625
contentValues .put (dbDataArrayList .get (i ).columnName , dbDataArrayList .get (i ).columnData .toString ());
1602
1626
}
1603
-
1604
- db .getWritableDatabase ().update (tableName , contentValues , whereClause , new String []{whereArgs });
1627
+
1628
+ // checking if column data was provided or not
1629
+ if (whereArgs != null && whereArgs .isEmpty ())
1630
+ {
1631
+ db .getWritableDatabase ().update (tableName , contentValues , whereClause , new String []{whereArgs });
1632
+ }
1633
+ else
1634
+ {
1635
+ // you can directly pass the values to where clause
1636
+ db .getWritableDatabase ().update (tableName , contentValues , whereClause , null );
1637
+ }
1638
+
1639
+ db .close ();
1605
1640
dbDataArrayList = new ArrayList <>();
1606
1641
1607
1642
return this ;
@@ -1629,7 +1664,10 @@ public boolean deleteTable(String tableName)
1629
1664
}
1630
1665
1631
1666
String query = "DELETE TABLE IF EXISTS " + tableName ;
1667
+
1632
1668
db .getWritableDatabase ().execSQL (query );
1669
+ db .close ();
1670
+
1633
1671
return true ;
1634
1672
}
1635
1673
catch (Exception e )
@@ -1699,7 +1737,7 @@ public <T> ArrayList<T> getAllRecords(String tableName, boolean isAscending,
1699
1737
Log .e (TAG , "getAllRecords: Select query for getting all records is: " + query );
1700
1738
1701
1739
// executing generated select query
1702
- cursor = db .getWritableDatabase ().rawQuery (query , null );
1740
+ cursor = db .getReadableDatabase ().rawQuery (query , null );
1703
1741
1704
1742
// checking if cursor is not null and cursor has moved to first position
1705
1743
if (cursor != null && cursor .moveToFirst ())
@@ -1871,7 +1909,7 @@ public <T> ArrayList<T> getAllRecords(String tableName, boolean isAscending,
1871
1909
Log .e (TAG , "getAllRecords: Select query for getting all records is: " + query );
1872
1910
1873
1911
// executing generated select query
1874
- cursor = db .getWritableDatabase ().rawQuery (query , null );
1912
+ cursor = db .getReadableDatabase ().rawQuery (query , null );
1875
1913
1876
1914
// checking if cursor is not null and cursor has moved to first position
1877
1915
if (cursor != null && cursor .moveToFirst ())
0 commit comments