Change Data Safely · practice
Update Only That Item
The preview confirms that EL-300 is the projector and its current period is 2.5. The next call should change that one 8 while leaving every other item alone:
change_loan_days(connection, "EL-300", 8)
The catalog_changes.py and find the new
def change_loan_days(connection, asset_tag, loan_days):
statement = """
UPDATE items
SET loan_days = ?
WHERE asset_tag = ?
"""
connection.execute(statement, (asset_tag, loan_days))
UPDATE items names the table to change. SET loan_days = ? gives the first placeholder the new value. WHERE asset_tag = ? makes the second placeholder identify the intended row. The changing statement carries its own precise
The starter
| SQL position | Python argument value |
|---|---|
first ?, new loan_days | loan_days |
second ?, target asset_tag | asset_tag |
Notice that the earlier preview and this update are two separate statements. Seeing the projector in the preview helped you decide what to do, but it does not limit a later update. The UPDATE must carry its own WHERE asset_tag = ?. When the call uses "EL-300", the database receives the statement unchanged and receives (8, "EL-300") as its values: 8 answers “what should change?” and "EL-300" answers “which row?”
That distinction becomes especially clear for a missing tag. (8, "NO-404") is still a valid pair of bound values, but the WHERE
Change the final tuple to (loan_days, asset_tag), then press Run.
before EL-300
(5, 'EL-300', 'Projector', 'electronics', 2.5)
change_loan_days(connection, "EL-300", 8)
None
after EL-300
(5, 'EL-300', 'Projector', 'electronics', 8)
The function still returns None for now. Read the before-and-after rows as the evidence: only the last value changed. Run also tries NO-404, which matches no row, and displays EL-400 afterward as an unchanged neighbor.
The table’s existing positive-period rule still applies. This function does not replace that rule or catch its error. It safely binds the proposed value; the database still decides whether the value satisfies the schema.
Submit uses unseen tags, a missing tag, apostrophes, SQL-looking text, and different positive whole and fractional periods. Keep both changing values outside the statement, in placeholder order. Do not filter by a non-unique name or category, and do not open another connection.
A safe change needs both halves you can now see: the new value in SET, and the unique identifying value in the same statement’s WHERE.
Task
In change_loan_days, correct the (loan_days, asset_tag). Keep the bound WHERE asset_tag = ? inside the UPDATE.
Run the project, compare the before-and-after row and unchanged neighbor, then submit it.