0

Possible Duplicate:
How to check existing database before creating new database on Android 2.2?

I have an app which check the existence of database in the start up. If not exits create a new one and if there then access the database. Can you please tell me how to check the existence of db(SQlite)?

Community
  • 1
  • 1
Arun M R
  • 41
  • 6
  • I think you can do it by using openorcreatedatabase function. See [this example](http://stackoverflow.com/questions/4645961/how-to-check-existing-database-before-creating-new-database-on-android-2-2); – Manoj Kumar Sep 14 '12 at 12:19

3 Answers3

0

I use a boolean flag which is set to true when onCreate of SQLiteOpenHelper is invoked. You can find my full code here

marwinXXII
  • 1,456
  • 14
  • 21
0

Android helps a lot developpers to manage a database. You should have a class like this (a single table with only 1 column) :

public class MyDBOpenHelper extends SQLiteOpenHelper {

private static final String queryCreationBdd = "CREATE TABLE partie (id INTEGER PRIMARY KEY)";

public MyDBOpenHelper(Context context, String name, CursorFactory factory, int version) 
{
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) 
{
    db.execSQL(queryCreationBdd);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    db.execSQL("DROP TABLE partie;");
    db.execSQL("DELETE FROM sqlite_sequence"); //table which contains the next incremented key value
    onCreate(db);
}

}

Then you simply do this :

MyDBOpenHelper databaseHelper = new MyDBOpenHelper(context, "dbname.db", null, 1);
SQLiteDatabase bdd = databaseHelper .getWritableDatabase();

If necessary, Android will create the database (call the onCreate method) or give you the one that already exists. The fourth parameter is the version of the database. If the currently created database is not the latest version, onUpgrade will be called.

EDIT : The database path will be something like this :

/data/data/fr.mathis.application/databases/dbname.db
Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
0

Take a look at query-if-android-database-exists


Open your database in try block with path of the databse like:

try{
   SQLiteDatabase   dbe = SQLiteDatabase.openDatabase("/data/data/bangla.rana.fahim/databases/dictionary", null,0);
            Log.d("opendb","EXIST");
            dbe.close();
            }

if an exception occurs then database doesn't exist so create it:

catch(SQLiteException e){
                Log.d("opendb","NOT EXIST");

            SQLiteDatabase db = openOrCreateDatabase("dictionary", MODE_PRIVATE, null);
                    db.execSQL("CREATE TABLE IF NOT EXISTS LIST(wlist varchar);");

                    db.execSQL("INSERT INTO LIST VALUES('খবর');");
                    db.execSQL("INSERT INTO LIST VALUES('কবর');"); //whatever you want
                 db.close();
}

that's it you are done :)

Community
  • 1
  • 1
Imran Rana
  • 11,899
  • 7
  • 45
  • 51