代码之家  ›  专栏  ›  技术社区  ›  Nikolas Bozic

Android不明确的列名

  •  0
  • Nikolas Bozic  · 技术社区  · 7 年前

    我有这个问题:

    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 回复  |  直到 7 年前
        1
  •  0
  •   CommonsWare    7 年前

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

    join table_doctor_type on dr_type=table_doctor_type.id
    

    问题是你有一个 dr_type 属于此查询一部分的多个表中的列。所以,在 dr_类型 使用包含特定 dr_类型 您感兴趣的列,例如:

    join table_doctor_type on users.dr_type=table_doctor_type.id