简体   繁体   中英

There is a foreign-key-syntax-error

I'm getting an error with this statement:

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

FATAL EXCEPTION: main Process: com.example.user.apple, PID: 3547 android.database.sqlite.SQLiteException: near "tcid": syntax error (code 1): , while compiling: 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 ) ;

Don't interleave FK constraints with column constraints; either use the in-line version:

mycolumn int references othertable

or explicit constraints after all columns:

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

It is optional to mention the column of the referenced table if the referenced column is the primary key (which you should define anyway).

So, change:

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

which has another syntax error with mismatched brackets, to:

CREATE TABLE trip (
   tid INTEGER PRIMARY KEY,
   ...
   tmid INTEGER REFERENCES member,
   tcid INTEGER REFERENCES carinfo,
   seats INTEGER
);
    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;
}

//this in datahelper class //using cursor to getid from another table

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM