From 6a8b12fa07198a2cca9746267617f3b247b4d4d6 Mon Sep 17 00:00:00 2001 From: amitjangid80 Date: Tue, 30 Jul 2019 16:08:25 +0530 Subject: [PATCH] 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. --- app/src/main/java/com/amit/db/DBHelper.java | 72 ++++++++++++++++----- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/amit/db/DBHelper.java b/app/src/main/java/com/amit/db/DBHelper.java index 133cc56..5fa98a6 100644 --- a/app/src/main/java/com/amit/db/DBHelper.java +++ b/app/src/main/java/com/amit/db/DBHelper.java @@ -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) @@ -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 @@ -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 @@ -551,7 +553,7 @@ public ArrayList 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 @@ -873,6 +875,8 @@ public boolean executeQuery(String query) if (query != null && !query.equalsIgnoreCase("")) { db.getWritableDatabase().execSQL(query); + db.close(); + return true; } else @@ -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; } @@ -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; @@ -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 { @@ -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; } @@ -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; } @@ -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) @@ -1235,6 +1254,7 @@ else if (columnData instanceof Double || columnData instanceof Float) db.getWritableDatabase().setTransactionSuccessful(); db.getWritableDatabase().endTransaction(); + db.close(); dbDataArrayList = new ArrayList<>(); } @@ -1386,6 +1406,7 @@ else if (columnData instanceof Double || columnData instanceof Float) db.getWritableDatabase().setTransactionSuccessful(); db.getWritableDatabase().endTransaction(); + db.close(); dbDataArrayList = new ArrayList<>(); } @@ -1456,7 +1477,7 @@ else if (object instanceof JSONArray) } } - this.insertDataWithTransaction(tableName, 5); + this.insertData(tableName); return true; } catch (Exception e) @@ -1565,6 +1586,11 @@ 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; } @@ -1572,17 +1598,15 @@ public DBHelper updateData(String tableName, String whereClause, String whereArg // 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) { @@ -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; @@ -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) @@ -1699,7 +1737,7 @@ public ArrayList 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()) @@ -1871,7 +1909,7 @@ public ArrayList 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())