7

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

例外を解決する方法に関する提案はありますか?

4

3 に答える 3

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)");

于 2012-05-09T12:50:13.830 に答える
2

getWritableDatabase()を試すことができます。このリンクが役に立ちますように。

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

于 2012-05-09T12:37:10.217 に答える
1

db.open メソッドを表示していませんでした... db を書き込み可能として開いていないに違いありません。次のようになります。

public GroceryDB open() throws SQLException {
    mDBHelper = new DBHelper(mCtx);
    mDb = mDBHelper.getWritableDatabase();
    return this;
}
于 2012-05-09T12:50:17.843 に答える