复合键,您的主键不是唯一的

时间:2014-01-24 02:21:02

标签: android composite-key sqlite

我有一个使用复合键作为主键的表。这是我的创建表代码。

private static final String db_createorderdtl = "create table  if not exists "
      + ORDER_DTL + "("
      + ORDER_ID +" varchar not null, "
      + COLUMN_ID+ " integer not null, "
      + QUANTITY + " varchar(3) not null," +
      " foreign key ("+ORDER_ID+") references "+TABLE_ORDER+"("+ORDER_ID+"),"+
      " foreign key ("+COLUMN_ID+") references "+TABLE_NAME+"("+COLUMN_ID+"), "+
      "primary key  ("+ORDER_ID+"," +COLUMN_ID+ "));";

当我在这个表中插入多个项目(相同的ORDER_ID,不同的COLUMN_ID)时,第一个项目没有问题,但是当插入第二个项目时,我有一个错误,告诉我的主键不是unique是指ORDER_ID。喜欢程序不明白我正在使用复合键。我应该在程序代码中声明一些内容吗?这是我插入的代码:

    public orderdtl createorderdtl(String orderid, String Id, String qty) {
    ContentValues values = new ContentValues();
    values.put(DBHelper.ORDER_ID, orderid);
    values.put(DBHelper.COLUMN_ID, Id);
    values.put(DBHelper.QUANTITY, qty);

    database.insert(DBHelper.ORDER_DTL, null,values);

    Cursor cursor = database.query(DBHelper.ORDER_DTL,
        allorderdtl, DBHelper.ORDER_ID + "=" + orderid, null,
        null, null, null);
    orderdtl neworderdtl = null;
    if (cursor.moveToFirst()) {
        neworderdtl = cursorToorderdtl(cursor);
    }
    cursor.close();
    return neworderdtl;
  }

0 个答案:

没有答案