创建表时Sqlite错误

时间:2016-12-16 12:31:50

标签: android android-sqlite

为什么我收到sqlite错误,感谢任何帮助

public static final String KEY_ORDERID="id_order";
    public static final  String KEY_SUBTRUCKID="id_subtruck";
    public static final  String KEY_TRUCK_NUMBER="id_trucknmbr";

private static final String CREATE_MASTER_TRUCK_DETAIL = "CREATE TABLE "
            + TABLE_TRUCK_DETAIL_MASTER_DATA + " ("
            + KEY_SUBTRUCKID + " TEXT,"
            + KEY_ORDERID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL"+")";

    private static final String CREATE_SLAVE_TRUCK_DETAIL = "CREATE TABLE "
        + TABLE_TRUCK_DETAIL_SLAVE_DATA+ " ("
        + KEY_ORDERID + " INTEGER,"
        + KEY_TRUCK_NUMBER + " TEXT,"
        + " FOREIGN KEY ("+KEY_SUBTRUCKID+") REFERENCES "
        +TABLE_TRUCK_DETAIL_MASTER_DATA+" ("+KEY_SUBTRUCKID+"))";

记录错误:

 unknown column "id_subtruck" in foreign key definition (code 1): , while compiling: CREATE TABLE truckslave (id_order INTEGER,id_trucknmbr TEXT, FOREIGN KEY (id_subtruck) REFERENCES truckmaster (id_subtruck))

1 个答案:

答案 0 :(得分:0)

CREATE_MASTER_TRUCK_DETAIL中的TABLE_TRUCK_DETAIL_MASTER_DATA更改为CREATE_SLAVE_TRUCK_DETAIL

    private static final String CREATE_SLAVE_TRUCK_DETAIL = "CREATE TABLE "
    + TABLE_TRUCK_DETAIL_SLAVE_DATA+ " ("
    + KEY_ORDERID + " INTEGER,"
    + KEY_TRUCK_NUMBER + " TEXT,"
    + KEY_SUBTRUCKID_SLAVE + " TEXT,"//Define new column which would be the foreign key
    + " FOREIGN KEY ("+KEY_SUBTRUCKID_SLAVE+") REFERENCES "
    + TABLE_TRUCK_DETAIL_MASTER_DATA +" ("+KEY_SUBTRUCKID+"))";

我认为您错误地附加了主表的 create语句来代替主表名

正如@ GVillani82指出的那样,您必须在TABLE_TRUCK_DETAIL_SLAVE_DATA中定义一个列,该列将引用KEY_SUBTRUCKID的{​​{1}}列。