The shop keeps its data in a database, a file called shop.db. Inside, the data sits in tables. A table is like an array of objects that all have the same keys: each row is one item, and each column is one of its keys. The products table has one row per product, with the columns id, name, price, category and stock.
Node's built-in node:sqlite module opens the file: new DatabaseSync("shop.db") gives back a connection, the line your program talks to the database through. Your questions are written in SQL, as a string. SELECT says which columns you want, and * means all of them; FROM says which table to read. This one reads every column of every customer:
const query = [
"SELECT *",
"FROM customers",
].join(" ");
The array holds one piece of the question per line, and .join(" ") glues the pieces into one string with a space between them, so query holds "SELECT * FROM customers". The space keeps the words apart. Each piece is one item of the array, so put a comma after it.
The check runs your query and prints the column names, the rows and how many there are. The gift card has no category, so that value is NULL, SQL's word for "no value", like null in JavaScript. Your code must print:
id | name | price | category | stock 1 | Mug | 12 | kitchen | 10 2 | Notebook | 7 | office | 0 3 | Lamp | 29 | home | 1 4 | Pen | 2 | office | 120 5 | Plant | 15 | home | 6 6 | Teapot | 22 | kitchen | 4 7 | Stapler | 9 | office | 15 8 | Gift card | 50 | NULL | 999 (8 rows)
Your task: open shop.db and keep the connection in a constant named db. Then write a query that reads every column of the products table.
Assignment
Follow the Coding Tutorial to open the shop's database and write your first query: every column, from the products table.
Hint
The file name is a string, so it goes in quotes. In the query, each piece is its own string, one per line inside the square brackets, with a comma after it.
Free plan: 30 tutor messages. PRO is unlimited.
