设置外键时,光标找不到列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设置外键时,光标找不到列相关的知识,希望对你有一定的参考价值。
我一直在尝试这个。我试图根据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;
}
答案
您的问题可能是由于输入错误,也许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);
Note
- 这是原则上的代码,它尚未经过测试,因此可能包含输入错误。
编辑
仔细检查后,您已有效编码的类表定义存在问题: -
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不是有效的关键字: -
因此你应该改变: -
// 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 " + ");";
到(*即从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 " + ");";
Note
- 此更改将要求删除并重新创建类表。
提供此功能的最简单方法是丢失所有数据的成本是删除数据库,在这种情况下,将调用
onCreate
方法(假设您使用的是SQLiteOpenHelper的子类,即DatabaseHelper)。 您可以清除/删除应用程序的数据,也可以卸载有效删除数据库的应用程序。
Additional
使用从上面的作品派生的以下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
即: -
以上是关于设置外键时,光标找不到列的主要内容,如果未能解决你的问题,请参考以下文章