Android模糊列名称

时间:2017-11-30 17:49:48

标签: android sqlite

我有这个问题:

SELECT users.name, users.surname, users.pin, users.telephone, users.email, table_doctor_type.dr_type, table_doctor_title.dr_title FROM users join table_doctor_type on dr_type=table_doctor_type.id join table_doctor_title on title_id=table_doctor_title.id

当我想执行此查询时,我收到此错误:

 Caused by: android.database.sqlite.SQLiteException: ambiguous column name: dr_type (code 1): , while compiling: SELECT users.name, users.surname, users.pin, users.telephone, users.email, table_doctor_type.dr_type, table_doctor_title.dr_title FROM users join table_doctor_type on dr_type=table_doctor_type.id join table_doctor_title on title_id=table_doctor_title.id

以下是我的表格:

 // TABLE USERS
private static final String TABLE_USERS = "users";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_SURNAME = "surname";
private static final String COLUMN_PIN = "pin";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_DR_TYPE= "dr_type";
private static final String COLUMN_EMAIL= "email";
private static final String COLUMN_TEL= "telephone";
private static final String COLUMN_USER_TYPE= "u_type";
 //TABLE DR TYPE
private static final String TABLE_DR_TYPE = "table_doctor_type";
private static final String DR_TYPE_COLUMN_ID = "id";
private static final String DR_TYPE_COLUMN_DR_TYPE = "dr_type";
// TABLE DOCTOR_TITLE
private static final String TABLE_DR_TITLE = "table_doctor_title";
private static final String DR_TITLE_COLUMN_ID = "id";
private static final String DR_TITLE_COLUMN_DR_TITLE = "dr_title";

 private static final String CREATE_TABLE_DR_TYPE = "CREATE TABLE "
        + TABLE_DR_TYPE + "("
        + DR_TYPE_COLUMN_ID + " integer primary key, "
        + DR_TYPE_COLUMN_DR_TYPE + " text not null);";

private static final String CREATE_TABLE_DR_TITLE= "CREATE TABLE "
        + TABLE_DR_TITLE + "("
        + DR_TITLE_COLUMN_ID + " integer primary key, "
        + DR_TITLE_COLUMN_DR_TITLE + " text not null);";

private static final String CREATE_TABLE_USER = "CREATE TABLE "
        + TABLE_USERS + " ("
        + COLUMN_ID + " integer primary key autoincrement, "
        + COLUMN_NAME + " text not null, "
        + COLUMN_SURNAME + " text not null, "
        + COLUMN_TITLE + " integer, "
        + COLUMN_DR_TYPE + " integer, "
        + COLUMN_PIN + " text not null, "
        + COLUMN_EMAIL + " text not null, "
        + COLUMN_TEL + " text not null, "
        + COLUMN_USER_TYPE + " text not null, "
        + " FOREIGN KEY ("+COLUMN_TITLE+") REFERENCES "+TABLE_DR_TITLE+"("+DR_TITLE_COLUMN_ID+"), "
        + " FOREIGN KEY ("+COLUMN_DR_TYPE+") REFERENCES "+TABLE_DR_TYPE+"("+DR_TYPE_COLUMN_ID+") "
        +");";

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

作为查询的一部分,您有:

join table_doctor_type on dr_type=table_doctor_type.id

问题是您在多个表中有一个dr_type列,该列是此查询的一部分。因此,在dr_type名称前加上包含您感兴趣的特定dr_type列的表名称,例如:

join table_doctor_type on users.dr_type=table_doctor_type.id