我覺得我差不多花時間在網絡上搜索可能的解決方案來解決我的問題。我成功地插入了一個學生和一個主題,但是當從兩個表中獲取數據時,它會導致程序崩潰並顯示錯誤「沒有這樣的列」。SQlite Android沒有這樣的表列students.studentId
請幫幫我。
創建表
private static final String CREATE_TABLE_SUBJECT = "CREATE TABLE "
+ TABLE_SUBJECT + "(" + SUBJECT_ID + " INTEGER PRIMARY KEY," + SUBJECT_NAME
+ " TEXT," + SUBJECT_SCHEDULE + " TEXT," + SUBJECT_FILELOCATION
+ " TEXT" + ")";
private static final String CREATE_TABLE_STUDENT = "CREATE TABLE "
+ TABLE_STUDENT + "(" + SUBJECT_ID + " INTEGER PRIMARY KEY," + STUDENT_NAME
+ " TEXT" + ")";
private static final String CREATE_TABLE_FUSION = "CREATE TABLE "
+ TABLE_FUSION + "(" + FUSION_ID + " INTEGER PRIMARY KEY," + SUBJECT_ID
+ " INTEGER," + STUDENT_ID +" INTEGER" + ")";
我從兩個表
顯示數據查詢public List<Student> getAllStudnetsBySubject(long id) {
List<Student> subject = new ArrayList<Student>();
String selectQuery = "SELECT * FROM " + TABLE_STUDENT + " students, "
+ TABLE_SUBJECT + " subjects, " + TABLE_FUSION + " fusions WHERE subjects."
+ SUBJECT_ID + " = '" + id + "'" + " AND subjects." + SUBJECT_ID
+ " = " + "fusions." + FUSION_ID + " AND students." + STUDENT_ID + " = "
+ "fusions." + FUSION_ID;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
Student s = new Student();
s.setStudentId(c.getInt((c.getColumnIndex(STUDENT_ID))));
s.setStudentName((c.getString(c.getColumnIndex(STUDENT_NAME))));
subject.add(s);
} while (c.moveToNext());
}
return subject;
}
logcat的錯誤
02-27 07:00:32.938: E/DatabaseHelper(20727): SELECT * FROM student students, subject subjects, fusion fusions WHERE subjects.subject_id = '1' AND subjects.subject_id = fusions.id AND students.student_id = fusions.id
02-27 07:00:32.948: E/SQLiteLog(20727): (1) no such column: students.student_id
woooh ..謝謝。我應該對它進行校對 – user3358155