使用外键创建数据库

时间:2013-11-01 22:03:46

标签: android-sqlite

我在这里寻求帮助,我已经知道如何创建数据库但现在我想要做的就是添加外键,错误就像log cat指示_Lien_parenté_CM附近的语法错误,解决方案是什么? / p>

 private static final String MENAGE = "table_MENAGE";

private static final String _id = "Numero_du_Questionnaire";
private static final String _idquest = "Numero_Question";
private static final String _idquesting = "Numero_Quest";
private static final String col_Code_district = "Code_district";
private static final String col_Region= "region";
private static final String col_Provence_prefecture  = "provence_prefecture";


private static final String Membres_de_Menage = "table_Membre_Menage";
private static final String col_N_Ordre = "Numero_Ordre";
private static final String col_Nom_prénom = "Nom_prénom";
private static final String col_Lien_parenté_CM = "Lien_parenté_CM";
private static final String col_Sexe  = "Sexe";
private static final String col_Age  = "Age ";
private static final String col_Langue_maternelle   = "Langue_maternelle ";
private static final String col_Lieu_naissance = "Lieu_naissance ";
private static final String col_Niveau_instruction  = "Niveau_instruction ";
private static final String col_branche_activité = "branche_activité ";

 android.database.sqlite.SQLiteException: near "Lien_parenté_CM": syntax error (code 1): , while compiling: CREATE TABLE table_Membre_Menage ( Numero_Ordre INT NOT NULL PRIMARY KEY, Nom_prénom TEXT,Numero_Quest integer,  FOREIGN KEY (Numero_Quest ) REFERENCES table_MENAGE (Numero_du_Questionnaire ),Lien_parenté_CM INT,Sexe INT,Age  INT,Langue_maternelle  INT,Lieu_naissance  INT,Niveau_instruction  INT,branche_activité  INT)

 private static final String DATABASE_CREATE1 = "CREATE TABLE " + MENAGE + "( " + _id +" INTEGER PRIMARY KEY AUTOINCREMENT,"
        + col_Region +" TEXT,"+  col_Provence_prefecture +" TEXT,"
 + col_Commune_Arrondissement +" TEXT,"+ col_N_district +" INT ");

  private static final String DATABASE_CREATE3 = "CREATE TABLE "+Membres_de_Menage+" ( " +col_N_Ordre+" INT NOT NULL PRIMARY KEY, "+col_Nom_prénom+" TEXT," + _idquesting + " integer, "
        + " FOREIGN KEY ("+ _idquesting +" ) REFERENCES "+MENAGE+" ("+_id+ ")," +col_Lien_parenté_CM+" INT,"+col_Sexe+" INT,"+col_Age+" INT,"+col_Langue_maternelle+" INT,"
 +col_Lieu_naissance+" INT,"+col_Niveau_instruction+" INT,"+col_branche_activité+" INT ");

1 个答案:

答案 0 :(得分:0)

这是SQLite命令:

CREATE TABLE table_Membre_Menage ( 
    Numero_Ordre INT NOT NULL PRIMARY KEY, 
    Nom_prénom TEXT,
    Numero_Quest integer,  
    FOREIGN KEY (Numero_Quest ) REFERENCES table_MENAGE (Numero_du_Questionnaire ),
    Lien_parenté_CM INT,      <-------- location of error
    Sexe INT,
    Age  INT,
    Langue_maternelle  INT,
    Lieu_naissance  INT,
    Niveau_instruction  INT,
    branche_activité  INT )

首先,我发现Android不允许您告诉SQLite强制执行外键约束(setForeignKeyConstraintsEnabled()),除非您只支持API 16+。所以拥有它们毫无用处。

其次,我仍然在我的代码中使用它们。但在所有字段定义之后,我将它们放在底部。我还在字段定义中声明了REFERENCES。

所以,试试这个:

更改

Numero_Quest integer, 

为:

Numero_Quest integer REFERENCES table_MENAGE (Numero_du_Questionnaire ),

然后,将FOREIGN KEY线放在底部。

这就是我能看到的与我正在做的不同。