import csv
import io
import sqlite3

source = io.StringIO("name,quantity\nNotebook,5\nPencil,12\n")
con = sqlite3.connect(":memory:", autocommit=False)
try:
    with con:
        con.execute("CREATE TABLE stock (name TEXT NOT NULL, quantity INTEGER CHECK(quantity >= 0))")
    with con:
        reader = csv.DictReader(source)
        if reader.fieldnames != ["name", "quantity"]:
            raise ValueError("Unexpected CSV headers")
        for row in reader:
            if not row["name"].strip():
                raise ValueError("Empty name")
            con.execute("INSERT INTO stock VALUES (?, ?)", (row["name"], int(row["quantity"])))
    print(con.execute("SELECT SUM(quantity) FROM stock").fetchone()[0])
finally:
    con.close()
