SELECT: Asking a Database Questions
So far your data vanished when the program ended. A database stores it and lets you ask questions in a language built for exactly that: SQL. Every backend, analytics stack and RAG metadata store speaks it, and it runs right here through Python's built-in sqlite3.
Data lives in tables (rows and columns). You read with SELECT:
SELECT name FROM products; -- one column
SELECT name, price FROM products; -- two
SELECT * FROM products; -- every columnIn Python you .execute(sql) on a connection and read the rows; each row is a tuple of the columns you asked for:
cur = con.execute("SELECT name FROM products")
for row in cur.fetchall():
print(row[0]) # the nameWrite all_names(con) that runs SELECT name FROM products and returns a plain list of the name strings, in row order. The connection is handed to you. Press Run.
Write all_names(con) that executes SELECT name FROM products and returns a list of the product name strings (e.g. ['Pen', 'Notebook', 'Lamp']), in row order. Unpack column 0 from each row.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.