存在外键 - 语法错误

时间:2017-02-21 17:03:43

标签: java sql foreign-keys syntax-error android-sqlite

我在这句话中收到错误:

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:3547                     android.database.sqlite.SQLiteException:near" tcid&#34 ;:语法错误(代码1):,编译时: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)),seat INTEGER);

3 个答案:

答案 0 :(得分:0)

不要将FK约束与列约束交错;要么使用内联版本:

mycolumn int references othertable
所有列后的

显式约束:

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

如果引用的列是主键(无论如何都应该定义),可以提及引用表的列。

所以,改变:

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

答案 1 :(得分: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;
}

答案 2 :(得分:0)

//这是datahelper类中的 //使用游标从另一个表中进行getid

True