引起:android.database.sqlite.SQLiteException:无法将只读数据库从版本0升级到1

时间:2012-05-09 12:07:47

标签: android android-sqlite

尝试创建SQLite数据库时,我收到以下LogCat异常:

  

引起:android.database.sqlite.SQLiteException:无法将只读数据库从版本0升级到1

这是我的代码:

import static android.provider.BaseColumns._ID;
import static org.inetizen.university_timetable.Constants.DAY_OF_WEEK;
import static org.inetizen.university_timetable.Constants.DURATION;
import static org.inetizen.university_timetable.Constants.MODULE_CODE;
import static org.inetizen.university_timetable.Constants.ROOM;
import static org.inetizen.university_timetable.Constants.START_TIME;
import static org.inetizen.university_timetable.Constants.TABLE_NAME;
import static org.inetizen.university_timetable.Constants.TYPE_OF_SESSION;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TimeTableData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "timetable.db";
private static final int DATABASE_VERSION = 1;

/** Create a helper object for the TimeTable database **/
public TimeTableData(Context ctx) {
    super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT " + MODULE_CODE
            + " TEXT NOT NULL " + DAY_OF_WEEK + " TEXT NOT NULL "
            + START_TIME + " INTEGER NOT NULL " + DURATION
            + " INTEGER NOT NULL " + TYPE_OF_SESSION + " TEXT NOT NULL "
            + ROOM + " TEXT NOT NULL )");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}
}

这是我的LogCat:

05-09 12:32:04.525: I/Database(601): sqlite returned: error code = 1, msg = near       "module_code": syntax error
05-09 12:32:04.525: E/Database(601): Failure 1 (near "module_code": syntax error) on   0x252588 when preparing 'CREATE TABLE table_name (_id INTEGER PRIMARY KEY AUTOINCREMENT     module_code TEXT NOT NULL day_of_week TEXT NOT NULL start_time INTEGER NOT NULL duration     INTEGER NOT NULL type_of_session TEXT NOT NULL room TEXT NOT NULL );'.
05-09 12:32:04.535: E/SQLiteOpenHelper(601): Couldn't open timetable.db for writing (will try read-only):    
05-09 12:32:04.535: E/SQLiteOpenHelper(601): android.database.sqlite.SQLiteException: near "module_code": syntax error: CREATE TABLE table_name (_id INTEGER PRIMARY KEY AUTOINCREMENT module_code TEXT NOT NULL day_of_week TEXT NOT NULL start_time INTEGER NOT NULL duration INTEGER NOT NULL type_of_session TEXT NOT NULL room TEXT NOT NULL );
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at org.inetizen.university_timetable.TimeTableData.onCreate(TimeTableData.java:26)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at org.inetizen.university_timetable.TimeTable.getClasses(TimeTable.java:57)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at org.inetizen.university_timetable.TimeTable.onCreate(TimeTable.java:46)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.os.Looper.loop(Looper.java:123)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at android.app.ActivityThread.main(ActivityThread.java:4627)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at java.lang.reflect.Method.invoke(Method.java:521)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-09 12:32:04.535: E/SQLiteOpenHelper(601):    at dalvik.system.NativeStart.main(Native Method)
05-09 12:32:04.545: D/AndroidRuntime(601): Shutting down VM
05-09 12:32:04.545: W/dalvikvm(601): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-09 12:32:04.555: E/AndroidRuntime(601): FATAL EXCEPTION: main
05-09 12:32:04.555: E/AndroidRuntime(601): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.inetizen.university_timetable/org.inetizen.university_timetable.TimeTable}: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 4: /data/data/org.inetizen.university_timetable/databases/timetable.db
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.os.Looper.loop(Looper.java:123)
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-09 12:32:04.555: E/AndroidRuntime(601):  at java.lang.reflect.Method.invokeNative(Native Method)
05-09 12:32:04.555: E/AndroidRuntime(601):  at java.lang.reflect.Method.invoke(Method.java:521)
05-09 12:32:04.555: E/AndroidRuntime(601):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-09 12:32:04.555: E/AndroidRuntime(601):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-09 12:32:04.555: E/AndroidRuntime(601):  at dalvik.system.NativeStart.main(Native Method)
05-09 12:32:04.555: E/AndroidRuntime(601): Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 4: /data/data/org.inetizen.university_timetable/databases/timetable.db
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:170)
05-09 12:32:04.555: E/AndroidRuntime(601):  at org.inetizen.university_timetable.TimeTable.getClasses(TimeTable.java:57)
05-09 12:32:04.555: E/AndroidRuntime(601):  at org.inetizen.university_timetable.TimeTable.onCreate(TimeTable.java:46)
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-09 12:32:04.555: E/AndroidRuntime(601):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-09 12:32:04.555: E/AndroidRuntime(601):  ... 11 more
05-09 12:32:07.325: I/Database(611): sqlite returned: error code = 1, msg = near "module_code": syntax error
05-09 12:32:07.325: E/Database(611): Failure 1 (near "module_code": syntax error) on 0x119778 when preparing 'CREATE TABLE table_name (_id INTEGER PRIMARY KEY AUTOINCREMENT module_code TEXT NOT NULL day_of_week TEXT NOT NULL start_time INTEGER NOT NULL duration INTEGER NOT NULL type_of_session TEXT NOT NULL room TEXT NOT NULL );'.
05-09 12:32:07.335: E/SQLiteOpenHelper(611): Couldn't open timetable.db for writing (will try read-only):
05-09 12:32:07.335: E/SQLiteOpenHelper(611): android.database.sqlite.SQLiteException: near "module_code": syntax error: CREATE TABLE table_name (_id INTEGER PRIMARY KEY AUTOINCREMENT module_code TEXT NOT NULL day_of_week TEXT NOT NULL start_time INTEGER NOT NULL duration INTEGER NOT NULL type_of_session TEXT NOT NULL room TEXT NOT NULL );
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at org.inetizen.university_timetable.TimeTableData.onCreate(TimeTableData.java:26)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at org.inetizen.university_timetable.TimeTable.getClasses(TimeTable.java:57)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at org.inetizen.university_timetable.TimeTable.onCreate(TimeTable.java:46)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.os.Looper.loop(Looper.java:123)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at android.app.ActivityThread.main(ActivityThread.java:4627)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at java.lang.reflect.Method.invoke(Method.java:521)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-09 12:32:07.335: E/SQLiteOpenHelper(611):    at dalvik.system.NativeStart.main(Native Method)
05-09 12:32:07.345: D/AndroidRuntime(611): Shutting down VM
05-09 12:32:07.345: W/dalvikvm(611): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-09 12:32:07.355: E/AndroidRuntime(611): FATAL EXCEPTION: main
05-09 12:32:07.355: E/AndroidRuntime(611): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.inetizen.university_timetable/org.inetizen.university_timetable.TimeTable}: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 4: /data/data/org.inetizen.university_timetable/databases/timetable.db
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.os.Looper.loop(Looper.java:123)
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-09 12:32:07.355: E/AndroidRuntime(611):  at java.lang.reflect.Method.invokeNative(Native Method)
05-09 12:32:07.355: E/AndroidRuntime(611):  at java.lang.reflect.Method.invoke(Method.java:521)
05-09 12:32:07.355: E/AndroidRuntime(611):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-09 12:32:07.355: E/AndroidRuntime(611):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-09 12:32:07.355: E/AndroidRuntime(611):  at dalvik.system.NativeStart.main(Native Method)
05-09 12:32:07.355: E/AndroidRuntime(611): Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 4: /data/data/org.inetizen.university_timetable/databases/timetable.db
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:170)
05-09 12:32:07.355: E/AndroidRuntime(611):  at org.inetizen.university_timetable.TimeTable.getClasses(TimeTable.java:57)
05-09 12:32:07.355: E/AndroidRuntime(611):  at org.inetizen.university_timetable.TimeTable.onCreate(TimeTable.java:46)
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-09 12:32:07.355: E/AndroidRuntime(611):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-09 12:32:07.355: E/AndroidRuntime(611):  ... 11 more

有关如何解决异常的任何建议?

3 个答案:

答案 0 :(得分:8)

您使用的SQL语法看起来不正确,您缺少逗号。试试这个:

db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + MODULE_CODE + " TEXT NOT NULL, " + DAY_OF_WEEK + " TEXT NOT NULL, " + START_TIME + " INTEGER NOT NULL, " + DURATION + " INTEGER NOT NULL, " + TYPE_OF_SESSION + " TEXT NOT NULL, " + ROOM + " TEXT NOT NULL)");

答案 1 :(得分:2)

您可以尝试 getWritableDatabase()。 这个链接可以帮到你。

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase%28%29

答案 2 :(得分:1)

你没有显示你的db.open方法......我敢打赌你没有把db打开成可写的。看起来应该是这样的:

public GroceryDB open() throws SQLException {
    mDBHelper = new DBHelper(mCtx);
    mDb = mDBHelper.getWritableDatabase();
    return this;
}