这是我的代码:
public class ResultDbHandler extends DatabaseHandler {
private final String TABLE_RESULTS = "results";
private final String RESULT_ORDER = "order";
private final String RESULT_PROFILE = "resultProfileId";
private final String RESULT_FVC = "fvc";
private final String RESULT_FEV1 = "fev1";
private final String RESULT_PEV = "pev";
private final String RESULT_TIME = "time";
public ResultDbHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String query = "CREATE TABLE " + TABLE_RESULTS + " ("
+ RESULT_ORDER + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ RESULT_PROFILE + " TEXT, "
+ RESULT_FVC + " REAL, "
+ RESULT_FEV1 + " REAL, "
+ RESULT_PEV + " REAL, "
+ RESULT_TIME + " TEXT, "
+ "FOREIGN KEY(" + RESULT_PROFILE + ") REFERENCES "
+ ProfileDbHandler.TABLE_PROFILES + "(" + ProfileDbHandler.PROFILE_ID + "))";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RESULTS);
onCreate(db);
}
@Override
public boolean isDbExist() {
return countRecord() > 0;
}
@Override
public int countRecord() {
SQLiteDatabase db = getReadableDatabase();
final String query = "SELECT count(*) FROM " + TABLE_RESULTS;
Cursor cursor = db.rawQuery(query, null);
int result = cursor.moveToFirst() ? cursor.getInt(0) : 0;
cursor.close();
db.close();
return result;
}
无论如何,DatabaseHandler
直接延伸SQLiteOpenHelper
。然后,我只需在初始化isDbExist()
对象(例如
dbHandler
...
dbHandler = new ResultDbHandler(activity);
dbHandler.isDbExist();
...
不知何故,应用程序崩溃了,logcat说:
09-09 22:34:58.904 4789-4789/? E/SQLiteLog: (1) no such table: results
09-09 22:34:58.907 4789-4789/? D/AndroidRuntime: Shutting down VM
09-09 22:34:58.910 4789-4789/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kahl.silir, PID: 4789
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kahl.silir/com.kahl.silir.main.history.MeasurementResultActivity}: android.database.sqlite.SQLiteException: no such table: results (code 1): , while compiling: SELECT count(*) FROM results
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: android.database.sqlite.SQLiteException: no such table: results (code 1): , while compiling: SELECT count(*) FROM results
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1346)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1285)
at com.kahl.silir.databasehandler.ResultDbHandler.countRecord(ResultDbHandler.java:64)
at com.kahl.silir.databasehandler.ResultDbHandler.isDbExist(ResultDbHandler.java:57)
at com.kahl.silir.main.history.MeasurementResultActivity.onCreate(MeasurementResultActivity.java:54)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
我不知道为什么来自onCreate
的{{1}}方法没有被调用,即使它表示该表不存在。我刚刚使用ResultDbHandler
的另一个直接子项在另一个活动中完成了相同的模式。它工作正常。请向我解释,为什么会发生这种情况以及如何解决?