Products Table CRUD
Using sqlite3, create a products table (id, name, price), insert the given products, then query and print all of them.
Approach: connect to an in-memory SQLite database, CREATE TABLE, INSERT each product with a parameterized query, then SELECT * (ordered by id for a predictable order) and print each row.
Input: First line: the number of products n. Next n lines: name,price.
Output: n lines: each product as a tuple (id, name, price).
3 Pen,10 Notebook,50 Bag,500
(1, 'Pen', 10.0) (2, 'Notebook', 50.0) (3, 'Bag', 500.0)
- 1 <= n <= 100
Hint 1
conn.execute("INSERT INTO products (name, price) VALUES (?, ?)", (name, price)) is the safe, parameterized way to insert.
Hint 2
Don't forget conn.commit() after inserting.
Hint 3
conn.execute("SELECT id, name, price FROM products ORDER BY id").fetchall() returns every row as a list of tuples, in a predictable order — print each one.
Each product is inserted with a parameterized INSERT (name, price as ? placeholders), then committed. SELECT id, name, price FROM products ORDER BY id retrieves every row in a predictable, insertion-matching order — printing each tuple from fetchall() gives exactly the required output.