【发布时间】:2018-08-12 13:24:53
【问题描述】:
我已经尝试这个很久了。我正在尝试根据 ID 从数据库中检索内容。在我将 INNER JOIN 和 WHERE 子句引入我的语句之前,一切都运行良好。我收到了这个错误
错误
android.database.sqlite.SQLiteException: no such column: classes_sections.classes_sections_ids (code 1): , while compiling: SELECT * FROM classes INNER JOIN classes_sections ON classes_sections.classes_sections_ids = classes.classes_sections_id WHERE classes.classes_id =2
我认为问题出在我的外键引用和我的查询中。我真的需要你的帮助。我被困在这一点上。谢谢。
这是我声明我在数据库中使用的常量的地方(FK 表示外键和 PK 主键)
//Classes Table Columns names
private static final String COLUMN_CLASSES_ID = "classes_id";
private static final String COLUMN_CLASS_ITEM_INDEX ="class_item_index";
private static final String COLUMN_CLASSES_NAME = "classes_name";
private static final String COLUMN_CLASSES_CODENAME = "classes_codename";
private static final String COLUMN_CLASSES_SECTIONS = "classes_sections_id";//FK
private static final String COLUMN_CLASSES_TEACHERS = "classes_teachers";//FK
private static final String COLUMN_CLASSES_STUDENTS = "classes_students";//FK
//Classes Sections Table Column names
private static final String COLUMN_CLASSES_SECTIONS_ID = "classes_sections_ids";//PK
private static final String COLUMN_CLASSES_SECTIONS_NAME = "classes_sections_name";
private static final String COLUMN_CLASSES_SECTIONS_DESCRIPTION = "classes_sections_description";
这是我的表格的代码
// create classes_table sql query
private String CREATE_CLASSES_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CLASSES + "("
+ COLUMN_CLASSES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_CLASS_ITEM_INDEX + " NUMBER,"
+ COLUMN_CLASSES_NAME + " VARCHAR," + COLUMN_CLASSES_CODENAME + " VARCHAR, " + COLUMN_CLASSES_SECTIONS + " INTEGER," + COLUMN_CLASSES_TEACHERS
+ " VARCHAR," + COLUMN_CLASSES_STUDENTS + " VARCHAR,"
+ "FOREIGN KEY(" + COLUMN_CLASSES_SECTIONS + " INTEGER) REFERENCES " + TABLE_CLASSES_SECTIONS + "(" + COLUMN_CLASSES_SECTIONS_ID + ") ON DELETE CASCADE " + ");";
//create sections sql query
private String CREATE_CLASSES_SECTIONS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CLASSES_SECTIONS + "("
+ COLUMN_CLASSES_SECTIONS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_CLASSES_SECTIONS_NAME + " VARCHAR,"
+ COLUMN_CLASSES_SECTIONS_DESCRIPTION + " VARCHAR" + ")";
此方法通过ID从数据库中检索数据
public ArrayList<SectionsBean> getAllSectionsByClassesID(long id){
String selectQuery = "SELECT * FROM "
+ "classes INNER JOIN classes_sections ON classes_sections.classes_sections_ids = classes.classes_sections_id WHERE classes.classes_id =" + id;
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<SectionsBean> sectionsBeanList = new ArrayList<SectionsBean>();
Cursor cursor = db.rawQuery(selectQuery,null);
Log.i("Query details", String.valueOf(cursor));
Log.d("DataDetails", DatabaseUtils.dumpCursorToString(cursor));
while (cursor.moveToNext()) {
ClassesBean classesBean = new ClassesBean();
classesBean.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_CLASSES_ID)));
classesBean.setClasses_name(cursor.getString(cursor.getColumnIndex(COLUMN_CLASSES_NAME)));
SectionsBean sectionsBean = new SectionsBean();
sectionsBean.setSectionsID(cursor.getLong(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_ID)));
sectionsBean.setSections_name(cursor.getString(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_NAME)));
sectionsBean.setClassesBean(classesBean);
sectionsBeanList.add(sectionsBean);
}
return sectionsBeanList;
}
【问题讨论】:
-
您好,如果您要输出 SQL 代码字符串,您/我们可以在 SQL 环境中尝试它们。例如 sqlfiddle.com。你的 DDL 在哪里?我们如何才能看到您是否定义了据说不存在的列?请阅读minimal reproducible example并采取行动。
标签: android database sqlite foreign-keys cursor