简体   繁体   English

存在外键语法错误

[英]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 ) ; 致命异常:主进程: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,提取文本,date INTEGER,time INTEGER,tmid INTEGER,FOREIGN KEY(tmid)参考成员(KEY_MID),tcid INTEGER,FOREIGN KEY(tcid)参考carinfo(KEY_CID INTE)), ;

Don't interleave FK constraints with column constraints; 不要将FK约束与列约束交织; 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 //这在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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