Search code examples
javaandroidsqlitemobile-application

how to find the particular db and table is already there or not in android


I'm a new android developer and developing an application that has a login screen.

When the application starts it checks if the database (LoginDB) exists or not. If it does not exist then I want to create the database, create the table and insert 1 row. If the database exists then it will continue the application execution.

How do you find out if a particular database exists and if the needed table is already there or not.

Im using android 2.2


Solution

  • To find the table is exists or not in the perticular db.

    SQLiteDatabase db = openOrCreateDatabase("your db file name",
                SQLiteDatabase.CREATE_IF_NECESSARY, null);
    Cursor cursor = db.query("sqlite_master", new String[] { "name" },
                    "name=" + "'your table name'", null, null, null, null);
    if (cursor.getCount() <= 0) {
       //Table is not exists
    }
    else 
    {
       //table is exists
    }