设置外键时,光标找不到列

时间:2018-03-04 22:24:30

标签: android database sqlite foreign-keys cursor

我一直在尝试这个问题。我试图根据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;

}

1 个答案:

答案 0 :(得分:1)

您的问题可能是由于输入错误造成的,可能classes_sections并不等同于TABLE_CLASSES_SECTIONS解析的内容。

我建议您始终只对所有数据库组件(数据库表列)使用单个源。

因此而不是: -

String selectQuery = "SELECT * FROM "
            + "classes INNER JOIN classes_sections ON classes_sections.classes_sections_ids = classes.classes_sections_id WHERE classes.classes_id =" + id;

你应该按照以下方式构建查询: -

String selectQuery = "SELECT * FROM " +
    TABLE_CLASSES +
    " INNER JOIN " + TABLE_CLASSES_SECTIONS +
    " ON " +
    TABLE_CLASSES_SECTIONS + "." + COLUMN_CLASSES_SECTIONS_ID +
    " = " + TABLE_CLASSES + "." + COLUMN_CLASSES_SECTIONS +
    " WHERE " + 
    TABLE_CLASSES + "." + COLUMN_CLASSES_SECTIONS +
    " = " + String.valueOf(id);

注意

  • 这是原则上的代码,尚未经过测试,因此可能包含输入错误。

修改

仔细检查后,表的定义也存在问题,因为您已有效编码: -

CREATE TABLE IF NOT EXISTS classes (
        classes_id INTEGER PRIMARY KEY AUTOINCREMENT,"
        class_item_index NUMBER,
        classes_name" VARCHAR,
        classes_codename VARCHAR, 
        classes_sections_id INTEGER,
        classes_teachers VARCHAR,
        classes_students VARCHAR,
        FOREIGN KEY(classes_sections_id INTEGER) REFERENCES classes_sections  (classes_sections_ids)  ON DELETE CASCADE);

表约束定义在语法上不正确,因为column_name参数接受包含1个或多个逗号分隔列名的列表。根据以下内容, INTEGER 不是有效的关键字: -

enter image description here

CREATE TABLE

因此你应该改变: -

// 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  " + ");";

to(*,即从column_names参数中删除 INTEGER ): -

 // 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 + ") REFERENCES " + TABLE_CLASSES_SECTIONS  + "(" + COLUMN_CLASSES_SECTIONS_ID + ")  ON DELETE CASCADE  " + ");";

注意

  • 此更改将要求删除并重新创建类表。
    • 最简单的方法是丢失所有数据,但是丢失所有数据的成本是删除数据库,在这种情况下,onCreate方法(假设你使用的是SQLiteOpenHelper的子类,也就是DatabaseHelper)将被调用
    • 您可以清除/删除应用程序的数据,也可以卸载有效删除数据库的应用程序。

其他

使用以下来自上述作品的SQL(语法上): -

DROP TABLE IF EXISTS classes;
CREATE TABLE IF NOT EXISTS classes (
        classes_id INTEGER PRIMARY KEY AUTOINCREMENT,
        class_item_index NUMBER,
        classes_name VARCHAR,
        classes_codename VARCHAR, 
        classes_sections_id INTEGER,
        classes_teachers VARCHAR,
        classes_students VARCHAR,
        FOREIGN KEY(classes_sections_id) REFERENCES classes_sections  (classes_sections_ids)  ON DELETE CASCADE);

DROP TABLE IF EXISTS classes_sections;
CREATE TABLE IF NOT EXISTS classes_sections (
        classes_sections_ids INTEGER PRIMARY KEY AUTOINCREMENT,
        classes_sections_name VARCHAR,
        classes_sections_description VARCHAR);
SELECT * FROM classes INNER JOIN classes_sections ON classes_sections.classes_sections_ids = classes.classes_sections_id WHERE classes.classes_id =2

即。 : -

enter image description here