【问题标题】:There is a foreign-key-syntax-error存在外键语法错误
【发布时间】:2017-02-21 17:03:43
【问题描述】:

我收到此语句的错误:

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:靠近“tcid”:语法错误(代码 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) ),seats INTEGER) ;

【问题讨论】:

  • 你在...之后多了一个右括号REFERENCES carinfo(KEY_CID)

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


【解决方案1】:

不要将 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
);

【讨论】:

  • 外键值为空或不引用父表中的值。请帮助
  • @thinker 你是什么意思?那是错误信息吗?您想在 FK 列中插入空值吗?听起来像是一个不同的问题,与此问题无关,因此您可能应该发布一个新问题。
  • 在我上面的行程表中,两个外键应该从我的两个父表中获取值。两个父表作为值,但外键值为空或 null。我应该怎么做才能获取值??
  • 或者我必须写 query 即使我已经引用了
  • 你可以插入一个空值,如果你知道如何将它链接回去,然后再更新它,或者在当时插入它,例如insert into trip (..., tmid, tcid, ...) values (..., (select KEY_MID from member where <whatever>), (select KEY_CID from carinfo where <whatever>), ...)
【解决方案2】:
    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;
}

【讨论】:

  • 请帮忙看看上面获取其他表的主键需要做哪些更改。
【解决方案3】:

//datahelper 类中的这个 //使用游标从另一个表中获取id

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

【讨论】:

  • 这是从父表中获取值但添加到前面的行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
相关资源
最近更新 更多