0

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

 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 ");
4

1 回答 1

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 行放在底部。

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

于 2013-11-02T00:10:06.783 回答