Skip to content

Commit

Permalink
1. Modified db helper functionality.
Browse files Browse the repository at this point in the history
2. Using readable when reading from db and using writable to write to db and closing the db after writing.
  • Loading branch information
amitjangid80 committed Jul 30, 2019
1 parent 9178f1c commit 6a8b12f
Showing 1 changed file with 55 additions and 17 deletions.
72 changes: 55 additions & 17 deletions app/src/main/java/com/amit/db/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ public boolean executeDatabaseOperations(String tableName, String operations,
// if successful then it will return true
// return true;
db.getWritableDatabase().execSQL(query);
db.close();

return true;
}
catch (Exception e)
Expand Down Expand Up @@ -353,7 +355,7 @@ public Cursor executeSelectQuery(String query)
try
{
// query execution
Cursor cursor = db.getWritableDatabase().rawQuery(query, null);
Cursor cursor = db.getReadableDatabase().rawQuery(query, null);

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

// executing query
cursor = db.getWritableDatabase().rawQuery(query, null);
cursor = db.getReadableDatabase().rawQuery(query, null);

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

// executing query
cursor = db.getWritableDatabase().rawQuery(query, null);
cursor = db.getReadableDatabase().rawQuery(query, null);

// if cursor is not null then moving the position to first
// and returning the cursor
Expand Down Expand Up @@ -873,6 +875,8 @@ public boolean executeQuery(String query)
if (query != null && !query.equalsIgnoreCase(""))
{
db.getWritableDatabase().execSQL(query);
db.close();

return true;
}
else
Expand Down Expand Up @@ -954,6 +958,11 @@ public DBHelper createTable(String tableName)
// checking if table name was provided or not
if (tableName == null || tableName.isEmpty())
{
if (dbColumnArrayList != null)
{
dbColumnArrayList.clear();
}

Log.e(TAG, "createTable: Table name was null or empty.");
return this;
}
Expand Down Expand Up @@ -992,6 +1001,8 @@ public DBHelper createTable(String tableName)
Log.e(TAG, "createTable: Create table query is: " + query.toString());

db.getWritableDatabase().execSQL(query.toString());
db.close();

dbColumnArrayList = new ArrayList<>();

return this;
Expand Down Expand Up @@ -1031,7 +1042,9 @@ public DBHelper alterTable(String tableName)
{
query = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + columnDataType;
Log.e(TAG, "alterTable: query for adding new column or altering table is: " + query);

db.getWritableDatabase().execSQL(query);
db.close();
}
else
{
Expand Down Expand Up @@ -1065,6 +1078,11 @@ public DBHelper insertData(String tableName)
// checking if table name was provided or not
if (tableName == null || tableName.isEmpty())
{
if (dbDataArrayList != null)
{
dbDataArrayList.clear();
}

Log.e(TAG, "insertData: Table name was null or empty.");
return this;
}
Expand All @@ -1089,8 +1107,9 @@ public DBHelper insertData(String tableName)

// executing inserting statement for inserting records in table
db.getWritableDatabase().insert(tableName, null, contentValues);
db.close();

dbDataArrayList = new ArrayList<>();

return this;
}

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

db.getWritableDatabase().setTransactionSuccessful();
db.getWritableDatabase().endTransaction();
db.close();

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

db.getWritableDatabase().setTransactionSuccessful();
db.getWritableDatabase().endTransaction();
db.close();

dbDataArrayList = new ArrayList<>();
}
Expand Down Expand Up @@ -1456,7 +1477,7 @@ else if (object instanceof JSONArray)
}
}

this.insertDataWithTransaction(tableName, 5);
this.insertData(tableName);
return true;
}
catch (Exception e)
Expand Down Expand Up @@ -1565,24 +1586,27 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
// checking if table name was provided or not
if (tableName == null || tableName.isEmpty())
{
if (dbDataArrayList != null)
{
dbDataArrayList.clear();
}

Log.e(TAG, "updateData: Table name was null or empty.");
return this;
}

// checking if column name was provided or not
if (whereClause == null || whereClause.isEmpty())
{
if (dbDataArrayList != null)
{
dbDataArrayList.clear();
}

Log.e(TAG, "updateData: Column name was null or empty.");
return this;
}

// checking if column data was provided or not
if (whereArgs == null || whereArgs.isEmpty())
{
Log.e(TAG, "updateData: Column data was null or empty.");
return this;
}

// checking if data was provided or not
if (dbDataArrayList == null || dbDataArrayList.size() == 0)
{
Expand All @@ -1600,8 +1624,19 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg
// adding column names and column data into content values
contentValues.put(dbDataArrayList.get(i).columnName, dbDataArrayList.get(i).columnData.toString());
}

db.getWritableDatabase().update(tableName, contentValues, whereClause, new String[]{whereArgs});

// checking if column data was provided or not
if (whereArgs != null && whereArgs.isEmpty())
{
db.getWritableDatabase().update(tableName, contentValues, whereClause, new String[]{whereArgs});
}
else
{
// you can directly pass the values to where clause
db.getWritableDatabase().update(tableName, contentValues, whereClause, null);
}

db.close();
dbDataArrayList = new ArrayList<>();

return this;
Expand Down Expand Up @@ -1629,7 +1664,10 @@ public boolean deleteTable(String tableName)
}

String query = "DELETE TABLE IF EXISTS " + tableName;

db.getWritableDatabase().execSQL(query);
db.close();

return true;
}
catch (Exception e)
Expand Down Expand Up @@ -1699,7 +1737,7 @@ public <T> ArrayList<T> getAllRecords(String tableName, boolean isAscending,
Log.e(TAG, "getAllRecords: Select query for getting all records is: " + query);

// executing generated select query
cursor = db.getWritableDatabase().rawQuery(query, null);
cursor = db.getReadableDatabase().rawQuery(query, null);

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

// executing generated select query
cursor = db.getWritableDatabase().rawQuery(query, null);
cursor = db.getReadableDatabase().rawQuery(query, null);

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

0 comments on commit 6a8b12f

Please sign in to comment.