我正在尝试创建一个名为 stock 的表,用于跟踪某人在购买一定数量后拥有多少股票。这是我迄今为止尝试过的最复杂的SQL代码,我认为我写错了东西。有人可以发现错误吗?
db.execute("IF EXISTS (SELECT * FROM stock WHERE symbol = :symbol AND userid = :userid)
UPDATE stock SET (amount = amount + 1) WHERE userid = :userid
ELSE INSERT INTO stock (userid, symbol, amount)
VALUES ((SELECT id FROM users WHERE id = :userid),
:symbol, :amount", userid = session["user_id"], symbol = symbol, amount = share_number)
您缺少括号
INSERT INTO stock (userid, symbol, amount)
VALUES ((SELECT id FROM users WHERE id = :userid), :symbol, :amount
而不是(SELECT id FROM users WHERE id = :userid)
你可以简单地写
INSERT INTO stock (userid, symbol, amount)
VALUES (:userid, :symbol, :amount)