Android中的SQLite数据库中没有创建表(没有这样的表...)

时间:2014-12-22 22:29:57

标签: android database sqlite

我使用两个SQLiteHelper类在数据库中创建两个不同的表:一个用于日志,另一个用于课程。我已经在课程中使用了on,它工作正常,但另一个人不想在数据库中创建一个表。当我使用insert方法插入日志表时,它不会给出任何错误,但是当我试图阅读它时,它会给出"没有这样的表"错误。然后,当我检查数据库中的哪些表时,只存在课程表。

这是SQLiteHelper课程的课程:

public class SQLiteHelperCourse extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";

public SQLiteHelperCourse(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
        + CourseEntry.TABLE_NAME + " (" + CourseEntry._ID
        + " INTEGER PRIMARY KEY, " + CourseEntry.COLUMN_NAME_COURSE_ID
        + TEXT_TYPE + COMMA_SEP + CourseEntry.COLUMN_NAME_COURSE_NAME
        + TEXT_TYPE + COMMA_SEP + CourseEntry.COLUMN_NAME_YEAR + TEXT_TYPE
        + COMMA_SEP + CourseEntry.COLUMN_NAME_SEMESTER + TEXT_TYPE
        + COMMA_SEP + CourseEntry.COLUMN_NAME_CREDITS + TEXT_TYPE + ")";

private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
        + CourseEntry.TABLE_NAME;

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

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onUpgrade(db, oldVersion, newVersion);
}

}

日志SQLiteHelper类:

public class SQLiteHelperLog extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";

public SQLiteHelperLog(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
        + LogEntry.TABLE_NAME + " (" + LogEntry._ID
        + " INTEGER PRIMARY KEY, " + LogEntry.COLUMN_NAME_COURSE
        + TEXT_TYPE + COMMA_SEP + LogEntry.COLUMN_NAME_START + TEXT_TYPE
        + COMMA_SEP + LogEntry.COLUMN_NAME_END + TEXT_TYPE + COMMA_SEP
        + LogEntry.COLUMN_NAME_DURATION + TEXT_TYPE + COMMA_SEP
        + LogEntry.COLUMN_NAME_COMMENT + TEXT_TYPE + ")";

private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
        + LogEntry.TABLE_NAME;

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

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onUpgrade(db, oldVersion, newVersion);
}

}

修改

LogCat Trace:

12-22 21:55:05.808: E/AndroidRuntime(3613): FATAL EXCEPTION: main
12-22 21:55:05.808: E/AndroidRuntime(3613): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.dt.aourz.example.timelogger/be.dt.aourz.example.timelogger.LogsListActivity}: android.database.sqlite.SQLiteException: no such table: logs (code 1): , while compiling: SELECT date, duration FROM logs WHERE course = 'Mobile Apps: Android'
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.os.Looper.loop(Looper.java:137)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at java.lang.reflect.Method.invoke(Method.java:511)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at dalvik.system.NativeStart.main(Native Method)
12-22 21:55:05.808: E/AndroidRuntime(3613): Caused by: android.database.sqlite.SQLiteException: no such table: logs (code 1): , while compiling: SELECT date, duration FROM logs WHERE course = 'Mobile Apps: Android'
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at be.dt.aourz.example.timelogger.LogsListActivity.onCreate(LogsListActivity.java:36)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.app.Activity.performCreate(Activity.java:5104)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-22 21:55:05.808: E/AndroidRuntime(3613):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-22 21:55:05.808: E/AndroidRuntime(3613):     ... 11 more

1 个答案:

答案 0 :(得分:2)

每个数据库文件只使用一个帮助程序类。 SQLiteOpenHelper版本数据库文件,而不是其内容。