So far your queries have read and changed tables that already existed. CREATE TABLE makes a new one. You give it a name and a list of columns, and it starts with no rows: those come later, with INSERT.
Each column is a name followed by its type, the kind of value it holds: INTEGER for whole numbers, TEXT for words. The columns sit inside parentheses with commas between them, so there is no comma after the last one. A table of discount coupons, for example:
CREATE TABLE coupons (
code TEXT,
percent INTEGER
)
In the editor, you build query one piece at a time: each query += "…"; line adds a string to its end, and the whole string is the SQL. The commas belong to the SQL, so they go inside the quotes.
The shop wants to keep its customers' reviews: which product, how many stars, and what the customer wrote. Once your table exists, the check lists its columns with their types, then adds two reviews and reads them back:
name | type id | INTEGER product_id | INTEGER stars | INTEGER text | TEXT (4 rows) id | product_id | stars | text 1 | 1 | 5 | Great mug 2 | 3 | 4 | Nice light (2 rows)
Your task: in run(), build a query that creates the table reviews with four columns, in this order: id, product_id and stars, all INTEGER, then text, a TEXT column.
Assignment
Follow the Coding Tutorial to create the reviews table, one column per line.
Hint
Until the last column is in, the program stops with an SQLiteException ending in near ")": syntax error: a table needs at least one column, and every comma must be followed by another column. Keep each comma inside the quotes: a comma after a closing quote is not valid Java, so the code doesn't compile.
Free plan: 30 tutor messages. PRO is unlimited.
