Jonas M F Banurea
Jonas M F Banurea

Reputation: 47

two foreign key in one tables sqlite android

I was making one table that have two foreign key to another tables. But still error in the query.

here the code :

    //inialitation attribute
    public static final String TABLE_MATRIKPENDAPAT = "tableMatrikPendapat";
    public static final String MATRIK_IDKRITERIA1 = "matrikIdKriteria1";
    public static final String MATRIK_IDKRITERIA2 = "matrikIdKriteria2";
    public static final String MATRIK_NILAI = "nilaiMatrik";

    //create query

    private static final String CREATE_TABLE_MATRIKPENDAPAT = "create table "
            + TABLE_MATRIKPENDAPAT + " ("
            + MATRIK_IDKRITERIA1 + " integer , FOREIGN KEY ("+ MATRIK_IDKRITERIA1 +") REFERENCES "
            + TABLE_KRITERIA + " ("+ KRITERIA_ID +"), " 
            + MATRIK_IDKRITERIA2 + " integer , FOREIGN KEY ("+ MATRIK_IDKRITERIA2 +") REFERENCES "
            + TABLE_KRITERIA + " ("+ KRITERIA_ID +"), "
            + MATRIK_NILAI + " integer not null);";

And the error said :

sqlite.SQLiteException: near "matrikIdKriteria2": syntax error

when i make it with with one foreign key, like here :

    private static final String CREATE_TABLE_MATRIKPENDAPAT = "create table "
    + TABLE_MATRIKPENDAPAT + " ("
    + MATRIK_IDKRITERIA1 + " integer , FOREIGN KEY ("+ MATRIK_IDKRITERIA1 +") REFERENCES "
    + TABLE_KRITERIA + " ("+ KRITERIA_ID +"), " 
    + MATRIK_NILAI + " integer not null);";

the error said :

sqlite.SQLiteException: near "nilaiMatrik": syntax error

so I think, my problems there in query after the first foreign key.

what should the correct query ?? Please help me guys

Upvotes: 0

Views: 2620

Answers (1)

James Black
James Black

Reputation: 41858

This:

integer , FOREIGN 

Should be

integer  FOREIGN 

With the comma it looks like another column.

Here is what I have:

   + BookTableMetaData.BOOK_USER_ID + " INTEGER REFERENCES "
   + UserTableMetaData.TABLE_NAME + "("
+ UserTableMetaData._ID + "),"

So, to have this work with your set up

MATRIK_IDKRITERIA1 + " integer REFERENCES "
+ TABLE_KRITERIA + " ("+ KRITERIA_ID +")

This may be enough.

Upvotes: 1

Related Questions