Databases with SQLite · 9 / 15 Exercise Medium

Update and delete

Objective

Create a Python program that modifies an inventory with UPDATE and DELETE, and shows how many rows each operation affects with cursor.rowcount. Use an in-memory database with the sqlite3 module (sqlite3.connect(":memory:")).

Instructions

  • Create the table inventory with item (text) and quantity (integer). Read n items with item quantity and store them.
  • Read m operations. If the line is - item k subtract that quantity from the item with UPDATE. If it is x item delete the item with DELETE.
  • After each operation show Affected rows: r (it is 0 if the item does not exist). At the end do commit().
  • Show Inventory: and each item as item: quantity, sorted by name.
Input
3
tornillo 10
clavo 5
sierra 1
4
- tornillo 3
- lima 2
x sierra
- clavo 5
Output
Affected rows: 1
Affected rows: 0
Affected rows: 1
Affected rows: 1
Inventory:
clavo: 0
tornillo: 7

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise