更新基于其他表创建的表(通过UNION ALL)

时间:2014-09-27 08:48:45

标签: android sqlite

我创建了一个最终表,通过 UNION ALL 语句with this Guide从其他两个表中获取数据 现在我有更新table final的新问题。
如果我想要更新“final_qty”,则必须更新“firts_qty”“second_qty”
但我不知道怎么办呢!
这是我的代码:

//表名

public static final String TBL_FIRST = "table_first"; 
public static final String TBL_SECOND = "table_second"; 
public static final String TBL_FINAL = "table_final"; 

//表Fisrt列

private static final String KEY_FIRST_ID = "first_id"; 
private static final String KEY_FIRST_PRODUCT = "first_product"; 
private static final String KEY_FIRST_QTY = "first_qty"; 

//表格第二列

private static final String KEY_SECOND_ID = "second_id"; 
private static final String KEY_SECOND_PRODUCT = "second_product"; 
private static final String KEY_SECOND_QTY = "second_qty"; 

//表格最终专栏

private static final String KEY_FINAL_ID = "final_id"; 
private static final String KEY_FINAL_PRODUCT = "final_product"; 
private static final String KEY_FINAL_QTY = "final_qty"; 

//创建语句

private static final String CREATE_TABLE_FIRST = "CREATE TABLE " + TBL_FIRST + "(" + 
KEY_FIRST_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT," + 
KEY_FIRST_PRODUCT + " TEXT," + 
KEY_FIRST_QTY + " INTEGER" + ")"; 

private static final String CREATE_TABLE_SECOND = "CREATE TABLE " + TBL_SECOND + "(" + 
KEY_SECOND_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT," + 
KEY_SECOND_PRODUCT + " TEXT," + 
KEY_SECOND_QTY + " INTEGER" + ")"; 

private static final String CREATE_TABLE_FINAL = "CREATE TABLE " + TBL_FINAL + "(" + 
KEY_FINAL_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT," + 
KEY_FINAL_PRODUCT + " TEXT," + 
KEY_FINAL_QTY + " INTEGER" + ")"; 

//将数据插入最终表

"INSERT INTO " + TBL_FINAL + "(" + 
KEY_FINAL_PRODUCT + "," + KEY_FINAL_QTY + ")" + 
" SELECT " + KEY_FIRST_PRODUCT + "," + KEY_FIRST_QTY +  
" FROM " + TBL_FIRST + " WHERE " + KEY_FIRST_QTY + ">" + 0 + 
"UNION ALL SELECT " + KEY_SECOND_PRODUCT + "," + KEY_SECOND_QTY + 
" FROM " + TBL_SECOND + " WHERE " + KEY_SECOND_QTY + ">" + 0  ;

这是我的活动样本。

Sample image

主要活动中,我可以转到第一个第二个最终活动。当我处于第一次第二次活动时,我会更改项目数量,然后返回主要活动
然后,我点击actionBar中的最终按钮,然后转到最终活动。在此活动中,我将更改项目数量,因此除 final_table first_table second_table 项目外数量必须更新。


此外,当我进入第一个活动并更新“first_qty”时,在我的“最终表格”中我有两个相同的行,其中的数量不同。

任何优惠都将受到赞赏。

0 个答案:

没有答案