final String CREATE_BASE_TABLE ="create table if not exists login ("
+ "id INTEGER PRIMARY KEY,"
+ "email TEXT,"
+ "password TEXT,"
+ ");";

I have an android app that needs to check if there’s already a database available in the device, and if not, process some things and eventually create it.Then check if a particular table exists.

SQLiteDatabase db;
db = openOrCreateDatabase(
"TestData.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);

If database exists and table exists simply read the data from the database if the data does exist.

Tip: For checking if a table Exists:

However, It is easy to find if a table exists or NOT,

Create a SQLiteDatabase object and have a call to query(…), the sql query string has select command. If the returned cursor is null then the table doesn’t exist.

SQLiteDatabase tempDatabase;

try {
tempDatabase =
SQLiteDatabase.openDatabase(DBPATH+DBNAME, null,
SQLiteDatabase.OPEN_READONLY);

try{

Cursor cur;

cur=tempDatabase.rawQuery("select * from table where id='"+idvar+";",null);

if(cur==null)

{

//our table doesn't exist, so we'll create one or take an action.

}

}catch (SQLiteException e) {
//our table doesn't exist, so we'll create one or take an action.
}

}
catch (SQLiteException e) {
//our database doesn't exist, so we'll create one or take an action.
}

Incoming search terms:

  • android sqlite query if table exists
  • how to check if table is exist android
  • how to check particular table exists or not in sqlite in iphone
  • how to detect if a table exists sql android
  • android check if database already exist
  • sqlite check if database exist
  • SQLite Create Table If Not Exists
  • sqlite database existence
  • test if sqlite table exists in android
  • how to check if table exist sqlite

Tagged with:

Filed under: Android

Like this post? Subscribe to my RSS feed and get loads more!