我使用以下查询在 Android SQLite 中创建了两个表:
public static final String TABLE_SEMESTER_CREATE = "CREATE TABLE "
+ SemesterTable.TABLE_NAME + " (" + SemesterTable.ID_PK
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + SemesterTable.TITLE
+ " TEXT," + SemesterTable.START_DATE + " TEXT, "
+ SemesterTable.END_DATE + " TEXT) ";
public static final String TABLE_COURSE_CREATE = "CREATE TABLE "
+ CourseTable.TABLE_NAME + " (" + CourseTable.ID_PK
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + CourseTable.COURSE_CODE
+ " TEXT, " + CourseTable.COURSE_NAME + " TEXT, "
+ CourseTable.LOCATION + " TEXT, " + CourseTable.SEMESTER_ID_FK
+ " INTEGER, " + "FOREIGN KEY (" + CourseTable.SEMESTER_ID_FK
+ ") REFERENCES " + SemesterTable.TABLE_NAME + " ("
+ SemesterTable.ID_PK + "));";
然后我在父表和子表中插入了一些数据。问题是当我执行查询时:
String query = "SELECT * FROM " + CourseTable.TABLE_NAME + " WHERE "
+ CourseTable.SEMESTER_ID_FK + " = 1;";
database.rawQuery(query, null);;
它不执行。你能帮我摆脱这个错误吗?我也试过了。
database.query(CourseTable.TABLE_NAME, CourseTable.allColumns,
CourseTable.SEMESTER_ID_FK + " = ? ",
new String[] { String.valueOf(semesterID) }, null, null, null);
但它也不起作用!