Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one check if a table exists in an Android SQLite database?

I have an android app that needs to check if there's already a record in the database, and if not, process some things and eventually insert it, and simply read the data from the database if the data does exist. I'm using a subclass of SQLiteOpenHelper to create and get a rewritable instance of SQLiteDatabase, which I thought automatically took care of creating the table if it didn't already exist (since the code to do that is in the onCreate(...) method).

However, when the table does NOT yet exist, and the first method ran upon the SQLiteDatabase object I have is a call to query(...), my logcat shows an error of "I/Database(26434): sqlite returned: error code = 1, msg = no such table: appdata", and sure enough, the appdata table isn't being created.

Any ideas on why?

I'm looking for either a method to test if the table exists (because if it doesn't, the data's certainly not in it, and I don't need to read it until I write to it, which seems to create the table properly), or a way to make sure that it gets created, and is just empty, in time for that first call to query(...)

EDIT
This was posted after the two answers below:
I think I may have found the problem. I for some reason decided that a different SQLiteOpenHelper was supposed to be created for each table, even though both access the same database file. I think refactoring that code to only use one OpenHelper, and creating both tables inside it's onCreate may work better...

like image 602
camperdave Avatar asked Jun 17 '10 04:06

camperdave


People also ask

How do you check if a table already exists in SQLite?

SELECT name FROM sqlite_master WHERE type='table' AND name='table_name'; where table_name in the where clause should be replaced with your table name. From the results of this query, you can check if there are any rows present in the result. If there is one row in the result, then the table exists.

How do I find a table in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.


1 Answers

Try this one:

public boolean isTableExists(String tableName, boolean openDb) {     if(openDb) {         if(mDatabase == null || !mDatabase.isOpen()) {             mDatabase = getReadableDatabase();         }          if(!mDatabase.isReadOnly()) {             mDatabase.close();             mDatabase = getReadableDatabase();         }     }      String query = "select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'";     try (Cursor cursor = mDatabase.rawQuery(query, null)) {         if(cursor!=null) {             if(cursor.getCount()>0) {                 return true;             }         }         return false;     } } 
like image 168
Nikolay DS Avatar answered Sep 27 '22 18:09

Nikolay DS