Skip to content

Commit 76aa141

Browse files
committed
1. Added query for checking if table exists in database.
2. New version 1.2.12 release
1 parent 184efe2 commit 76aa141

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,56 @@ else if (conditionalValues != null)
512512
return 0;
513513
}
514514
}
515+
516+
/**
517+
* 2018 September 07 - Friday - 05:39 PM
518+
* is table exists method
519+
*
520+
* this method will check if a table exists in database
521+
* if yes is will not execute create table query
522+
* else it will execute
523+
*
524+
* @param tableName - name of the table to check if that table exists or not
525+
*
526+
* @return true - if table exists in database
527+
* false - if table not exists in database
528+
**/
529+
public boolean isTableExists(String tableName)
530+
{
531+
try
532+
{
533+
// query for checking if table exists in database
534+
String query = "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name = '" + tableName + "'";
535+
536+
// executing the query using cursor
537+
Cursor cursor = db.getReadableDatabase().rawQuery(query, null);
538+
539+
// checking if cursor is not null
540+
if (cursor != null)
541+
{
542+
// cursor is not null, moving the cursor position to first
543+
cursor.moveToFirst();
544+
545+
// getting the count from cursor
546+
int count = cursor.getCount();
547+
548+
// closing the cursor
549+
cursor.close();
550+
551+
// returning true if table exists in database
552+
// else false if table does not exists in database
553+
return count > 0;
554+
}
555+
else
556+
{
557+
return false;
558+
}
559+
}
560+
catch (Exception e)
561+
{
562+
Log.e(TAG, "isTableExists: exception in is table exists method:\n");
563+
e.printStackTrace();
564+
return false;
565+
}
566+
}
515567
}

0 commit comments

Comments
 (0)