SQLite异常:没有这样的列

时间:2014-04-08 22:43:23

标签: android database sqlite crash

我正在制作一个时间表应用程序,我希望从数据库中获取一系列房间详细信息(例如H21),搜索与日,周,课程和时间匹配的条目。主题。

我一直收到错误,没有这样的列“房间”,环顾四周我可以看到这很容易由SQL中的错误引起。我找不到任何错误,但我会感激任何帮助。

调用方法的行(day,wk,l和temp都被指定为适当的值):

TimingsDb tDb = new TimingsDb(this);
TextView tvTab1tvRoom1 = (TextView) findViewById(R.id.textViewCLW1tvRoom1);


tvTab1tvRoom1.setText(tDb.getRoom(day,wk,l,temp));

和TimingsDb数据库:

public static final String KEY_ID = "_id";
public static final String KEY_SUBJECT = "subject_name";
public static final String KEY_WEEK = "week";
public static final String KEY_DAY = "day_name";
public static final String KEY_LESSON = "lesson";
public static final String KEY_ROOM = "room";
public static final String KEY_TEACHER = "teacher";

private static final String DATABASE_NAME = "TimingsDB";
private static final String DATABASE_TABLE = "TimingsTable";
private static final int DATABASE_VERSION = 1;  

@Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SUBJECT
                + " TEXT, " + KEY_WEEK + " INTEGER, "
                + KEY_DAY + " TEXT, " + KEY_LESSON + " INTEGER, "
                + KEY_ROOM + " TEXT, " + KEY_TEACHER + " TEXT);");
    }// end onCreate

public String getRoom(String day, String week, String lesson, String subject){
    String[] column = new String[] { KEY_ROOM };
    String selection = KEY_DAY + " = ? AND " + KEY_WEEK + " = ? AND "
            + KEY_LESSON + " = ? AND " + KEY_SUBJECT + " = ?";
    String[] selectionArgs = new String[] { day, week, lesson, subject };
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection,
            selectionArgs, null, null, null);
    String result = "";
    if (c.moveToFirst()) {
        int iRoom = c.getColumnIndex(KEY_ROOM);
        result = c.getString(iRoom);
    }
    c.close();
    if (result == null) {
        result = "";
    }
    return result;

}//end getRoom

LogCat报告:

04-08 23:39:53.997: E/SQLiteLog(24200): (1) no such column: room
04-08 23:39:53.997: D/AndroidRuntime(24200): Shutting down VM
04-08 23:39:53.997: W/dalvikvm(24200): threadid=1: thread exiting with uncaught exception (group=0x41df3ac8)
04-08 23:39:53.997: E/AndroidRuntime(24200): FATAL EXCEPTION: main
04-08 23:39:53.997: E/AndroidRuntime(24200): java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.shearer.schooltimetable/com.shearer.schooltimetable.CurrentLesson}: android.database.sqlite.SQLiteException: no such column: room (code 1): , while compiling: SELECT room FROM TimingsTable WHERE day_name = ? AND week = ? AND lesson = ? AND subject_name = ?
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.app.ActivityThread.access$600(ActivityThread.java:146)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.os.Looper.loop(Looper.java:137)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.app.ActivityThread.main(ActivityThread.java:5171)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at java.lang.reflect.Method.invokeNative(Native Method)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at java.lang.reflect.Method.invoke(Method.java:511)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at dalvik.system.NativeStart.main(Native Method)
04-08 23:39:53.997: E/AndroidRuntime(24200):    Caused by: android.database.sqlite.SQLiteException: no such column: room (code 1): , while compiling: SELECT room FROM TimingsTable WHERE day_name = ? AND week = ? AND lesson = ? AND subject_name = ?
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:949)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:560)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1342)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1189)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1060)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1228)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at com.shearer.schooltimetable.TimingsDb.getRoom(TimingsDb.java:136)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at com.shearer.schooltimetable.CurrentLesson.getLessons(CurrentLesson.java:123)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at com.shearer.schooltimetable.CurrentLesson.onCreate(CurrentLesson.java:53)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.app.Activity.performCreate(Activity.java:5200)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-08 23:39:53.997: E/AndroidRuntime(24200):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-08 23:39:53.997: E/AndroidRuntime(24200):    ... 11 more

2 个答案:

答案 0 :(得分:1)

更新您的数据库版本:

private static final int DATABASE_VERSION = 2;

我想你以后添加了你的专栏KEY_ROOM。

答案 1 :(得分:0)

将您的数据库版本更改为2。

private static final int DATABASE_VERSION = 2;

并使用

String room = c.getString(5); 而不是

   int iRoom = c.getColumnIndex(KEY_ROOM)

     result = c.getString(iRoom);