来自其他表的Sqlite数据库插入数据仅插入一行

时间:2019-04-13 12:48:45

标签: java android sqlite

从包含3行且quote_id = 1的行的产品表插入数据时,我遇到了问题,我只能从产品到价格表中获取第一个数据。

要插入价格的代码

db.execsql("insert into price (name, quantity ) values ((select from product title, quantity where quote_id =1 )  )   " )

已更新:

我想在同一条语句中插入一个静态ID; 值= 1;

insert into price (price_id,name, quantity,row_total) 
"+value+",select title, quantity,SUM(price*quantity) 
from product where quote_id = 1

我想实现这个目标

3 个答案:

答案 0 :(得分:1)

您使用的语法不正确。试试这个:

insert into price (price_id, name, quantity) 
select 1, title, quantity 
from product 
where quote_id = 1

在这种情况下,您必须不要使用关键字values
这将返回where quote_id = 1表中的所有行product并将它们插入price表中。
如果仅返回1行,则将插入这1行。
price表中是否还有其他约束不允许插入更多行,例如唯一索引?
编辑

db.execsql(
    "insert into price (price_id, name, quantity) select " + 
     value + 
    ", title, quantity from product where quote_id = 1");

答案 1 :(得分:0)

如果“ quote_id”是价格表的主键,而产品表中的“ quote_id”是相同的,那么您就是在覆盖相同的记录。

如果不是这种情况,那么我建议查询产品,以确保有多个quote_id = 1的记录。

答案 2 :(得分:0)

db.execsql(“在价格(名称,数量)中插入标题,从quote_id = 1的产品中选择数量”)

相关问题