Skip to content

Commit 6a8b12f

Browse files
committed
1. Modified db helper functionality.
2. Using readable when reading from db and using writable to write to db and closing the db after writing.
1 parent 9178f1c commit 6a8b12f

File tree

1 file changed

+55
-17
lines changed

1 file changed

+55
-17
lines changed

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

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ public boolean executeDatabaseOperations(String tableName, String operations,
326326
// if successful then it will return true
327327
// return true;
328328
db.getWritableDatabase().execSQL(query);
329+
db.close();
330+
329331
return true;
330332
}
331333
catch (Exception e)
@@ -353,7 +355,7 @@ public Cursor executeSelectQuery(String query)
353355
try
354356
{
355357
// query execution
356-
Cursor cursor = db.getWritableDatabase().rawQuery(query, null);
358+
Cursor cursor = db.getReadableDatabase().rawQuery(query, null);
357359

358360
// if cursor is not null then moving the position to first
359361
// and returning the cursor
@@ -448,7 +450,7 @@ public Cursor executeSelectQuery(String tableName, String values,
448450
}
449451

450452
// executing query
451-
cursor = db.getWritableDatabase().rawQuery(query, null);
453+
cursor = db.getReadableDatabase().rawQuery(query, null);
452454

453455
// if cursor is not null then moving the position to first
454456
// and returning the cursor
@@ -551,7 +553,7 @@ public <T> ArrayList<T> executeSelectQuery(String tableName, String values,
551553
}
552554

553555
// executing query
554-
cursor = db.getWritableDatabase().rawQuery(query, null);
556+
cursor = db.getReadableDatabase().rawQuery(query, null);
555557

556558
// if cursor is not null then moving the position to first
557559
// and returning the cursor
@@ -873,6 +875,8 @@ public boolean executeQuery(String query)
873875
if (query != null && !query.equalsIgnoreCase(""))
874876
{
875877
db.getWritableDatabase().execSQL(query);
878+
db.close();
879+
876880
return true;
877881
}
878882
else
@@ -954,6 +958,11 @@ public DBHelper createTable(String tableName)
954958
// checking if table name was provided or not
955959
if (tableName == null || tableName.isEmpty())
956960
{
961+
if (dbColumnArrayList != null)
962+
{
963+
dbColumnArrayList.clear();
964+
}
965+
957966
Log.e(TAG, "createTable: Table name was null or empty.");
958967
return this;
959968
}
@@ -992,6 +1001,8 @@ public DBHelper createTable(String tableName)
9921001
Log.e(TAG, "createTable: Create table query is: " + query.toString());
9931002

9941003
db.getWritableDatabase().execSQL(query.toString());
1004+
db.close();
1005+
9951006
dbColumnArrayList = new ArrayList<>();
9961007

9971008
return this;
@@ -1031,7 +1042,9 @@ public DBHelper alterTable(String tableName)
10311042
{
10321043
query = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + columnDataType;
10331044
Log.e(TAG, "alterTable: query for adding new column or altering table is: " + query);
1045+
10341046
db.getWritableDatabase().execSQL(query);
1047+
db.close();
10351048
}
10361049
else
10371050
{
@@ -1065,6 +1078,11 @@ public DBHelper insertData(String tableName)
10651078
// checking if table name was provided or not
10661079
if (tableName == null || tableName.isEmpty())
10671080
{
1081+
if (dbDataArrayList != null)
1082+
{
1083+
dbDataArrayList.clear();
1084+
}
1085+
10681086
Log.e(TAG, "insertData: Table name was null or empty.");
10691087
return this;
10701088
}
@@ -1089,8 +1107,9 @@ public DBHelper insertData(String tableName)
10891107

10901108
// executing inserting statement for inserting records in table
10911109
db.getWritableDatabase().insert(tableName, null, contentValues);
1110+
db.close();
1111+
10921112
dbDataArrayList = new ArrayList<>();
1093-
10941113
return this;
10951114
}
10961115

@@ -1103,7 +1122,7 @@ public DBHelper insertData(String tableName)
11031122
*
11041123
* this method will insert data into table using database transaction
11051124
* this method is useful for inserting bulk records into table in less time
1106-
**/
1125+
**/
11071126
//#endregion COMMENTS FOR insertDataWithTransaction method
11081127
@Deprecated
11091128
public void insertDataWithTransaction(String tableName)
@@ -1235,6 +1254,7 @@ else if (columnData instanceof Double || columnData instanceof Float)
12351254

12361255
db.getWritableDatabase().setTransactionSuccessful();
12371256
db.getWritableDatabase().endTransaction();
1257+
db.close();
12381258

12391259
dbDataArrayList = new ArrayList<>();
12401260
}
@@ -1386,6 +1406,7 @@ else if (columnData instanceof Double || columnData instanceof Float)
13861406

13871407
db.getWritableDatabase().setTransactionSuccessful();
13881408
db.getWritableDatabase().endTransaction();
1409+
db.close();
13891410

13901411
dbDataArrayList = new ArrayList<>();
13911412
}
@@ -1456,7 +1477,7 @@ else if (object instanceof JSONArray)
14561477
}
14571478
}
14581479

1459-
this.insertDataWithTransaction(tableName, 5);
1480+
this.insertData(tableName);
14601481
return true;
14611482
}
14621483
catch (Exception e)
@@ -1565,24 +1586,27 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
15651586
// checking if table name was provided or not
15661587
if (tableName == null || tableName.isEmpty())
15671588
{
1589+
if (dbDataArrayList != null)
1590+
{
1591+
dbDataArrayList.clear();
1592+
}
1593+
15681594
Log.e(TAG, "updateData: Table name was null or empty.");
15691595
return this;
15701596
}
15711597

15721598
// checking if column name was provided or not
15731599
if (whereClause == null || whereClause.isEmpty())
15741600
{
1601+
if (dbDataArrayList != null)
1602+
{
1603+
dbDataArrayList.clear();
1604+
}
1605+
15751606
Log.e(TAG, "updateData: Column name was null or empty.");
15761607
return this;
15771608
}
15781609

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-
15861610
// checking if data was provided or not
15871611
if (dbDataArrayList == null || dbDataArrayList.size() == 0)
15881612
{
@@ -1600,8 +1624,19 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
16001624
// adding column names and column data into content values
16011625
contentValues.put(dbDataArrayList.get(i).columnName, dbDataArrayList.get(i).columnData.toString());
16021626
}
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();
16051640
dbDataArrayList = new ArrayList<>();
16061641

16071642
return this;
@@ -1629,7 +1664,10 @@ public boolean deleteTable(String tableName)
16291664
}
16301665

16311666
String query = "DELETE TABLE IF EXISTS " + tableName;
1667+
16321668
db.getWritableDatabase().execSQL(query);
1669+
db.close();
1670+
16331671
return true;
16341672
}
16351673
catch (Exception e)
@@ -1699,7 +1737,7 @@ public <T> ArrayList<T> getAllRecords(String tableName, boolean isAscending,
16991737
Log.e(TAG, "getAllRecords: Select query for getting all records is: " + query);
17001738

17011739
// executing generated select query
1702-
cursor = db.getWritableDatabase().rawQuery(query, null);
1740+
cursor = db.getReadableDatabase().rawQuery(query, null);
17031741

17041742
// checking if cursor is not null and cursor has moved to first position
17051743
if (cursor != null && cursor.moveToFirst())
@@ -1871,7 +1909,7 @@ public <T> ArrayList<T> getAllRecords(String tableName, boolean isAscending,
18711909
Log.e(TAG, "getAllRecords: Select query for getting all records is: " + query);
18721910

18731911
// executing generated select query
1874-
cursor = db.getWritableDatabase().rawQuery(query, null);
1912+
cursor = db.getReadableDatabase().rawQuery(query, null);
18751913

18761914
// checking if cursor is not null and cursor has moved to first position
18771915
if (cursor != null && cursor.moveToFirst())

0 commit comments

Comments
 (0)