我正在运行此功能:
public int getSubjectId(int level, String subject){
SQLiteDatabase db = this.getWritableDatabase();
Cursor subjectCursor = db.rawQuery("select * from " + SUBJECTS_TABLE_NAME + " where "+ SUBJECTS_COL_2 + " = " + subject + " and " + SUBJECTS_COL_3 + "="+level,null);
ArrayList<Integer> mArrayList = new ArrayList<Integer>();
for(subjectCursor.moveToFirst(); !subjectCursor.isAfterLast(); subjectCursor.moveToNext()) {
mArrayList.add(subjectCursor.getInt(0));
}
return mArrayList.get(0);
}
尝试返回表中第一个项的id,其中第2列和第3列都与函数参数匹配。但是当它运行时,我收到一条错误消息,因为该函数正在搜索输入主题名称的列而不是包含该值的行。
我收到错误:
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: Maths (code 1):
通常我会使用预准备语句修复此问题,但我已经读过,当您尝试返回一个值时不应该使用它们,在这种情况下我就像我想要返回ArrayList一样。也许我在这种情况下误解并准备好了陈述,但无论哪种方式,我都会感谢任何帮助找到我得到的错误的最佳解决方案。
由于
编辑:
SQLiteDatabase db = this.getWritableDatabase();
String sql = "select * from " + SUBJECTS_TABLE_NAME + " where "+ SUBJECTS_COL_2 + " =?" + " and " + SUBJECTS_COL_3 + "=? ";
SQLiteStatement statement = db.compileStatement(sql);
statement.bindString(1, subject);
statement.bindString(2, level);
Cursor subjectCursor = statement.???();