I need to check existing database before creating new database on android 2.2. How to check it?
Asked
Active
Viewed 3,282 times
4 Answers
1
use openOrCreateDatabase
method
----- 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;
}

Sarwar Erfan
- 18,034
- 5
- 46
- 57
-
But got exception if database is not exist means. How to resolve this issue? – bharath Jan 10 '11 at 12:21
-
I have edited the answer and added the method I use to check existence. – Sarwar Erfan Jan 10 '11 at 13:44
1
Doesn't it work with the DatabaseHelper ? If you haven't tried here is code I posted before...
-
http://mfarhan133.wordpress.com/2010/10/24/database-crud-tutorial-for-android/ I used this sample. Its working fine. But At begining i got the exception if database is not exist means. see the DBAdapter.java class – bharath Jan 10 '11 at 12:55
-
Are you getting an IOException? Maybe you can post some code to look at. Because for me it is working fine if no DB is available. – Beasly Jan 10 '11 at 12:58
0
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.

Hkh
- 357
- 1
- 10
-1
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();
}

Tony Barajas
- 124
- 2
- 12