我使用以下查询在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 + "));";
然后我在Parent表和子表中插入了一些数据。问题是当我执行查询时:
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);
但它也无效!
答案 0 :(得分:2)
问题是char ;
是不允许的。因此,请更正您的查询:
String query = "SELECT * FROM " + CourseTable.TABLE_NAME + " WHERE "
+ CourseTable.SEMESTER_ID_FK + " = ?";
database.rawQuery(query, new String[] {String.valueOf(1)});
现在它会起作用。我建议您使用名为 ?
的 placeholder
,并将替换为 String-array
作为rawQuery()
方法的第二个参数。
同时从 ;
TABLE_COURSE_CREATE
中删除最后的 String
字符。
注意:始终使用它,使用此方法可以避免任何问题。
答案 1 :(得分:1)
两个提示,以增加获得有用答案的机会(或更好,自己解决问题):
答案 2 :(得分:0)
你错过了一个等号吗?您的第一个查询是评估类似:
SELECT * FROM TABLE WHERE FIELD1
改变这个:
"SELECT * FROM " + CourseTable.TABLE_NAME +
" WHERE " + CourseTable.SEMESTER_ID_FK + "1;"
要:
"SELECT * FROM " + CourseTable.TABLE_NAME +
" WHERE " + CourseTable.SEMESTER_ID_FK + "=1;"