我需要检查我的应用程序中是否存在预先存在的数据库,为此我编写了以下代码:
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try {
String myPath = "/data/data/com.mycomp.sampleapp/databases/myDB.db";
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
}
catch (Exception e) { }
return checkDB != null ? true : false;
}
上面的代码按预期工作。现在我的问题是它在我的Logcat中的异常语句下打印。我只想做什么,不想在我的Logcat中打印异常。
E/SQLiteDatabase(27655): Failed to open database '/data/data/com.mycomp.sampleapp/databases/myDB.db'.
E/SQLiteDatabase(27655): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:338)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:232)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:512)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:885)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:855)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
E/SQLiteDatabase(27655): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
E/SQLiteDatabase(27655): at com.mycomp.sampleapp.DataBaseHelper.checkDataBase(DataBaseHelper.java:63)
E/SQLiteDatabase(27655): at com.mycomp.sampleapp.GetData.doInBackground(GetData.java:55)
E/SQLiteDatabase(27655): at com.mycomp.sampleapp.GetData.doInBackground(GetData.java:1)
E/SQLiteDatabase(27655): at android.os.AsyncTask$2.call(AsyncTask.java:288)
E/SQLiteDatabase(27655): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/SQLiteDatabase(27655): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/SQLiteDatabase(27655): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/SQLiteDatabase(27655): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/SQLiteDatabase(27655): at java.lang.Thread.run(Thread.java:841)
我已经在try / catch块中编写了代码,但它仍然在我的日志文件中打印了异常。
异常是从我的代码的以下语句生成的:
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
请告诉我如何防止日志文件中出现打印异常。
答案 0 :(得分:2)
您可以检查文件是否存在:
File dbFile = mContext.getDatabasePath(myPath);
boolean exists = dbFile.exists();
对于你的例子:
private static final String DB_PATH = "myDB.db";
public boolean checkDataBase() {
File dbFile = mContext.getDatabasePath(DB_PATH);
return dbFile.exists();
}