在数据库中找不到Android SQLite ForeignKey列

时间:2016-09-01 05:07:12

标签: android sqlite android-studio foreign-key-relationship android-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);



}

2 个答案:

答案 0 :(得分:2)

因为您尚未在survey_key_id

中添加CREATE_FORM_DETAILS_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),"
            + Survey_KEY_ID + " INTEGER,"
            + " FOREIGN KEY ("+Survey_KEY_ID+") REFERENCES "+Table_Survey+"("+Survey_KEY_ID+"));";
    db.execSQL(CREATE_FORM_DETAILS_TABLE);

答案 1 :(得分:2)

您现在正在将相同的表列映射到外键。

 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);
}

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); }