Android 在数据库中找不到 SQLite 外键列

Android SQLite ForeignKey Column not found in Database

程序成功运行后,我从 Android 设备监视器中提取 .db 文件,我可以找到我所有的表和每个表的列,除了 Foreign key 的列。可以任何人为什么会这样?我的 Query 有问题吗?这是我的代码:

 private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "datacollector.db";

private static final String Table_Survey = "SURVEY";
private static final String Survey_KEY_ID = "id";
private static final String Survey_KEY_NAME = "Survey_Name";
private static final String KEY_Table_Name = "Table_Name";


private static final String Table_formDetails = "FormDetails";
private static final String formDetails_KEY_ID = "id";
private static final String formDetails_field_Type="Field_Type";
private static final String formDetails_label = "Label";
private static final String formDetails_Options = "Options";

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_SURVEY_TABLE = "CREATE TABLE " + Table_Survey + "("
            + Survey_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + Survey_KEY_NAME + " VARCHAR(30),"
            + KEY_Table_Name + " VARCHAR(50)" + ")";
    db.execSQL(CREATE_SURVEY_TABLE);


    String CREATE_FORM_DETAILS_TABLE = "CREATE TABLE " + Table_formDetails + "("
            + formDetails_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + formDetails_field_Type + " VARCHAR(30),"
            + formDetails_Options + " VARCHAR(30),"
            + formDetails_label + " VARCHAR(30),"
            + " FOREIGN KEY ("+Survey_KEY_ID+") REFERENCES "+Table_Survey+"("+Survey_KEY_ID+"));";
    db.execSQL(CREATE_FORM_DETAILS_TABLE);



}

这是因为您还没有在 CREATE_FORM_DETAILS_TABLE

中添加 survey_key_id
String CREATE_FORM_DETAILS_TABLE = "CREATE TABLE " + Table_formDetails + "("
            + formDetails_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + formDetails_field_Type + " VARCHAR(30),"
            + formDetails_Options + " VARCHAR(30),"
            + formDetails_label + " VARCHAR(30),"
            + Survey_KEY_ID + " INTEGER,"
            + " FOREIGN KEY ("+Survey_KEY_ID+") REFERENCES "+Table_Survey+"("+Survey_KEY_ID+"));";
    db.execSQL(CREATE_FORM_DETAILS_TABLE);

您现在正在将相同的 table 列映射到外部 key.try。 </p> <pre><code> private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "datacollector.db"; private static final String Table_Survey = "SURVEY"; private static final String Survey_KEY_ID = "id"; private static final String Survey_KEY_NAME = "Survey_Name"; private static final String KEY_Table_Name = "Table_Name"; private static final String Table_formDetails = "FormDetails"; private static final String formDetails_KEY_ID = "id"; private static final String formDetails_field_Type="Field_Type"; private static final String formDetails_label = "Label"; private static final String formDetails_Options = "Options"; public DbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_SURVEY_TABLE = "CREATE TABLE " + Table_Survey + "(" + Survey_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + Survey_KEY_NAME + " VARCHAR(30)," + KEY_Table_Name + " VARCHAR(50)" + ")"; db.execSQL(CREATE_SURVEY_TABLE); String CREATE_FORM_DETAILS_TABLE = "CREATE TABLE " + Table_formDetails + "(" + formDetails_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + formDetails_field_Type + " VARCHAR(30)," + formDetails_Options + " VARCHAR(30)," + formDetails_label + " VARCHAR(30)," + forign_Survey_KEY_ID + "INTEGER DEFAULT 0," + " FOREIGN KEY ("+forign_Survey_KEY_ID+") REFERENCES "+Table_Survey+"("+Survey_KEY_ID+"));"; db.execSQL(CREATE_FORM_DETAILS_TABLE); }