gpt4 book ai didi

android - SQLite异常: No Such Column

转载 作者:行者123 更新时间:2023-12-02 01:07:59 24 4
gpt4 key购买 nike

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

我不断收到一个错误,指出没有这样的列“rooms”,环顾四周,我发现这很容易由 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

最佳答案

更新您的数据库版本:

private static final int DATABASE_VERSION = 2; 

我认为您稍后添加了KEY_ROOM列。

关于android - SQLite异常: No Such Column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22949635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com