2017-02-21 10 views
0

あり:外部キー構文エラーが、私はこの文でエラーを取得しています

CREATE TABLE trip (
    tid INTEGER PRIMARY KEY, 
    from_trip TEXT, 
    to_trip TEXT, 
    seat_cost INTEGER, 
    pickup TEXT, 
    date INTEGER, 
    time INTEGER, 
    tmid INTEGER, 
    FOREIGN KEY (tmid) REFERENCES member(KEY_MID), 
    tcid INTEGER, 
    FOREIGN KEY (tcid) REFERENCES carinfo(KEY_CID)), 
    seats INTEGER 
); 

致命的な例外:メイン プロセス:com.example.user.apple、PID:android.database.sqlite.SQLiteException: "tcid"の近く:構文エラー(コード1):コンパイル中に:CREATE TABLEトリップ(tid INTEGER PRIMARY KEY、from_trip TEXT、to_trip TEXT、seat_cost INTEGER、pickup TEXT、date INTEGER、時刻INTEGER、tmid INTEGER、FOREIGN KEY(tmid)REFERENCESメンバー(KEY_MID)、tcid INTEGER、外部キー(tcid)関連項目carinfo(KEY_CID))、座席INTEGER);

+0

あなた後ろに余分な閉じ括弧があります。 '参照carinfo(KEY_CID)' –

答えて

0

FK制約と列制約をインターリーブしないでください。参照列がある場合には、参照されるテーブルの列に言及することはオプションです

mycol1 int, 
mycol2 int, 
foreign key (mycol1) references othertable, 
foreign ket (mycol2) references othertable2 (somecol) 

mycolumn int references othertable 

または明示的な制約すべて列:イン・ラインバージョンを使用しますか主キー(とにかく定義する必要があります)。だから、

、変更:

に、不一致ブラケットで別の構文エラーがあり
CREATE TABLE trip (
    tid INTEGER PRIMARY KEY, 
    ... 
    tmid INTEGER, 
    FOREIGN KEY (tmid) REFERENCES member(KEY_MID), 
    tcid INTEGER, 
    FOREIGN KEY (tcid) REFERENCES carinfo(KEY_CID)), 
    seats INTEGER 
); 

CREATE TABLE trip (
    tid INTEGER PRIMARY KEY, 
    ... 
    tmid INTEGER REFERENCES member, 
    tcid INTEGER REFERENCES carinfo, 
    seats INTEGER 
); 
+0

外部キー値がnullであるか、親テーブルから値をリフレッシュしません。 – Thinker

+0

@thinkerどういう意味ですか?それはエラーメッセージですか? FK列にNULLを挿入しますか?この質問には関係のない別の質問のように聞こえるので、おそらく新しい質問を投稿するべきです。 – Bohemian

+0

私の上記の旅行テーブルでは、2つの外部キーは、私の2つの親テーブルから2つの親テーブルの値を取得する必要がありますが、外部キー値は空またはnull.What値を取得する必要がありますか? – Thinker

0
public long createride(String from_trip,String to_trip,String date,String time,String pickup,String cost,String seats) { 

    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_FROM,from_trip); 
    values.put(KEY_TO,to_trip); 
    values.put(KEY_DATE,date); 
    values.put(KEY_TIME,time); 
    values.put(KEY_PICKUP,pickup); 
    values.put(KEY_SEATS,seats); 
    values.put(KEY_SEAT_COST,cost); 
    long insertId = db.insert(TABLE_TRIP, null, values); 

    return insertId; 
} 
+0

他のテーブルの主キーを取得する上記の。 – Thinker

0

//このdatahelperクラス //からのgetIdカーソルを使用して別のテーブル

public Cursor getId(String tableName) 
    { 

    String query = ("insert into trip tmid select mid from member"); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor c = db.rawQuery(query,null); 
    return c; 
    } 
//this in my main activity 
//calling cursor 
      public void onClick(View v) { 
      long isInserted = myDb.createride(etfrom.getText().toString(), 
        etto.getText().toString(), 
        etdate.getText().toString(), 
        ettime.getText().toString(), 
        etpick.getText().toString(), 
        etcost.getText().toString(), 
        etseat.getText().toString()); 
      if (isInserted > 0){ 

       Cursor cursor = myDb.getId(myDb.TABLE_MEMBER); 
       startManagingCursor(cursor); 

       while(cursor.move(0)) { 
        int id = cursor.getInt(0); 
       } 
    //this is how i using to get value to table 
     public long createride(String from_trip,String to_trip,String date,String time,String pickup,String cost,String seats) { 

    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_FROM,from_trip); 
    values.put(KEY_TO,to_trip); 
    values.put(KEY_DATE,date); 
    values.put(KEY_TIME,time); 
    values.put(KEY_PICKUP,pickup); 
    values.put(KEY_SEATS,seats); 
    values.put(KEY_SEAT_COST,cost); 

    long insertId = db.insert(TABLE_TRIP, null, values); 

    return insertId; 
} 
+0

これは親テーブルから値を取得していますが、前の行に追加する – Thinker

+0

それが正しくない場合は助けてください – Thinker

関連する問題