Lecture 17 - Introduction to SQL with SQLite
sqlite3 module (we’ll see this later)..sqlite or .db files directly within VSCode.Ctrl+Shift+X or Cmd+Shift+X) and search for “SQLite”. Install the one by alexcvzz..sqlite or .db files (it will create one if it doesn’t exist)..sql files or the query editor.
Searching for the SQLite extension in VSCode.
lecture17.sqlite in the current directory (lectures/lecture-17/). This file will hold our database.17-introduction-sql.sql (or use the existing one if you prefer, just clear its contents). This will be our SQL script file.17-introduction-sql.sql file.Choose Database) and select Attach existing database. Choose the lecture17.sqlite file you just created..sql file will be executed against the lecture17.sqlite database file.
Connecting the .sql file to the SQLite database file.
.sql file is connected to the lecture17.sqlite database, we can write and run SQL commands.-- Drop the table if it already exists
DROP TABLE IF EXISTS drivers;
-- Create the drivers table
CREATE TABLE drivers(
driver_id INTEGER PRIMARY KEY, -- Use INTEGER PRIMARY KEY for auto-increment in SQLite
driver_name TEXT, -- TEXT is common for strings in SQLite
nationality TEXT,
victories INTEGER -- INTEGER is the standard type for integers
);DROP TABLE IF EXISTS drivers;: Removes the drivers table if it exists.CREATE TABLE drivers(...): Creates an empty TABLE called drivers.(...), we define the columns:
driver_id INTEGER PRIMARY KEY: An integer column that uniquely identifies each driver. In SQLite, this often acts as an auto-incrementing key.driver_name TEXT: A column to store the driver’s name as text.nationality TEXT: A column for the driver’s nationality.victories INTEGER: A column for the number of victories.17-introduction-sql.sql editor.Run Query or use the shortcut (Ctrl+Shift+Q or Cmd+Shift+Q).SQLite output panel for success messages.INSERT INTO command.VALUES to the table.;)..sql file:-- Insert data into the drivers table
INSERT INTO drivers (driver_name, nationality, victories) VALUES ('Lewis Hamilton','British', 103);
INSERT INTO drivers (driver_name, nationality, victories) VALUES ('Fernando Alonso', 'Spanish', 32);
INSERT INTO drivers (driver_name, nationality, victories) VALUES ('Sebastian Vettel', 'German', 91); -- Note: Original had 91, corrected from 53
INSERT INTO drivers (driver_name, nationality, victories) VALUES ('Michael Schumacher', 'German', 53); -- Note: Original had 53, corrected from 91Note: We don’t specify driver_id here; SQLite will auto-generate it because it’s an INTEGER PRIMARY KEY.
INSERT statements in your .sql file and run them using Run Query.SELECT command!SELECT soon, but for now let’s run the following command in our .sql file:*) from the drivers table.Ctrl+Shift+Q or Cmd+Shift+Q).
Result of SELECT FROM drivers.*
drivers table in your 17-introduction-sql.sql file.driver_name, nationality, and victories.INSERT statements.SELECT * FROM drivers; again to see the updated table.INSERT INTO drivers (driver_name, nationality, victories) VALUES ('Max Verstappen', 'Dutch', 51);
INSERT INTO drivers (driver_name, nationality, victories) VALUES ('Juan Pablo Montoya', 'Colombian', 6);
INSERT INTO drivers (driver_name, nationality, victories) VALUES ('Danilo Freire','Brazilian', 10); -- Let's be optimistic! 😉.sql file to our lecture17.sqlite database and run queries..sql file editor.Run Query or use the shortcut (Ctrl+Shift+Q or Cmd+Shift+Q).
Running a query from the .sql file.
lecture17.sqlite database listed under the “SQLite Explorer” panel.drivers), their columns, and data types.Show Table) or generating SELECT statements.
Using the SQLite Explorer in VSCode.
| driver_id | driver_name | nationality | victories |
|---|---|---|---|
| 1 | Lewis Hamilton | British | 103 |
| 2 | Fernando Alonso | Spanish | 32 |
| 3 | Sebastian Vettel | German | 91 |
| 4 | Michael Schumacher | German | 53 |
| 5 | Max Verstappen | Dutch | 51 |
| 6 | Juan Pablo Montoya | Colombian | 6 |
| 7 | Danilo Freire | Brazilian | 10 |
Note: The exact driver_id values might differ if you dropped and recreated the table multiple times, but the relative order should be similar.