如何在一个查询中删除多个表?

时间:2019-03-17 17:36:11

标签: java android sql sqlite

我需要使用相同的ID删除两个表。到目前为止,我正在使用这种方法:

public Integer deleteSplitMondayByID (String ID){
        SQLiteDatabase database = this.getWritableDatabase();
        return database.delete(TABLE_NAME, "ID = ?", new String[]{ID});
    }

如何在其中添加一张桌子?我已经尝试过

(TABLE_NAME, TABLE_NAME2 "ID = ?", new String[]{ID})
(TABLE_NAME + " , " + TABLE_NAME2 "ID = ?", new String[]{ID})
(TABLE_NAME + " UNION " + TABLE_NAME2 "ID = ?", new String[]{ID})

但是我无法删除它们。那么,我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

如上所述,您不能在一个语句中从两个表中删除,尤其是在Java库中抽象该表时。尝试这样的事情:

public Integer deleteSplitMondayByID (String ID){
  int delcount = 0;
  SQLiteDatabase database = this.getWritableDatabase();
  database.beginTransaction();
  try 
    {
      delcount += database.delete(TABLE_NAME, "ID = ?", new String[]{ID});
      delcount += database.delete(TABLE_NAME2, "ID = ?", new String[]{ID});
      database.setTransactionSuccessful();
    } finally {         
      database.endTransaction();
    }
  return delcount;
}