Update and Delete by ID
Write functions updateprice(productid, newprice) and deleteproduct(product_id) using parameterized queries, then apply one update and one delete to a products table and print what's left.
Approach: build the products table as in the previous problem, run an UPDATE ... WHERE id = ? and a DELETE ... WHERE id = ?, then select and print the remaining rows.
Input: First line: the number of products n. Next n lines: name,price. Then: the id to update, the new price, and the id to delete.
Output: The remaining rows (after the update and the delete), each as a tuple (id, name, price).
3 Pen,10 Notebook,50 Bag,500 2 599 1
(2, 'Notebook', 599.0) (3, 'Bag', 500.0)
- 1 <= n <= 100
Hint 1
update_price should run UPDATE products SET price = ? WHERE id = ?, then commit.
Hint 2
delete_product should run DELETE FROM products WHERE id = ?, then commit.
Hint 3
Always filter with WHERE id = ? — without it, every row would be changed or removed.
updateprice runs UPDATE products SET price = ? WHERE id = ? with the new price and target id as parameters, committing afterward. deleteproduct runs DELETE FROM products WHERE id = ? the same way. Both rely on the WHERE clause to target exactly one row — omitting it would affect the whole table.