2

i have a table MY_DATABASE_TABLE .. but i want to know if it exists or not

public boolean checkDataBase() { SQLiteDatabase checkDB = null; try { checkDB = SQLiteDatabase.openDatabase(MY_DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY); checkDB.close(); } catch (SQLiteException e) { // database doesn't exist yet. } return checkDB != null ? true : false; } 

the above code didn't worked and always returned false

2
  • 1
    What exactly do you want to know? If the database itself exists (as the code tries to do) - or if a table in the database exists as the subjects says? Commented Apr 22, 2011 at 11:39
  • Your code is correct, the problem should be your path, sorry that you mark the other answer as correct. Very poor solution BTW Commented Sep 1, 2015 at 3:14

2 Answers 2

7
SELECT COUNT() FROM sqlite_master WHERE name ='NAME_OF_YOUR_TABLE'; 
Sign up to request clarification or add additional context in comments.

2 Comments

Easiest and bad as well
What is sqlite_master in your answer? May be i was wrong, it should be the name of the table and WHERE name ='NAME_OF_YOUR_TABLE' here name will be the column name and 'NAME_OF_YOUR_TABLE' this will be value of name column.
1

At the moment you're not checking if a table exists. You're opening a database. Check these: http://notes.theorbis.net/2008/12/check-if-datablase-table-exists.html http://groups.google.com/group/android-developers/browse_thread/thread/ffbe5bcdfbf5acaa

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.