bharath
bharath

Reputation: 14453

How to check existing database before creating new database on android 2.2?

I need to check existing database before creating new database on android 2.2. How to check it?

Upvotes: 2

Views: 3291

Answers (4)

Hkh
Hkh

Reputation: 357

You should need to check database already exists or not, if not than create database else not create database. Please you can use below query.

CREATE TABLE if not exists TABLE_NAME (key data_type);

Call this query inside onCreate method.

Upvotes: 0

Tony Barajas
Tony Barajas

Reputation: 124

To check if your database was created you can use the following code and it will not be recreated every time you open the application. dbName = is the name of you DB

public static boolean doesDatabaseExist(Context context, String dbName) {
        File dbFile = context.getDatabasePath(dbName);
        return dbFile.exists();
    }

Upvotes: -1

Sarwar Erfan
Sarwar Erfan

Reputation: 18068

use openOrCreateDatabase method

Read here

----- EDIT ------

public boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);

    }catch(SQLiteException e){

        //database does't exist yet.
    }

    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

Upvotes: 1

Beasly
Beasly

Reputation: 1537

Doesn't it work with the DatabaseHelper ? If you haven't tried here is code I posted before...

Android - Sqlite database method undefined fot type

Upvotes: 1

Related Questions