Create a Database and Table
In this first step, we'll create a SQLite database and a table to store user data. This will provide the foundation for exploring transaction handling in subsequent steps.
First, open your terminal in the LabEx VM. Your default path is /home/labex/project
.
Now, let's create a SQLite database named mydatabase.db
. Run the following command to create the database file and open the SQLite command-line tool:
sqlite3 mydatabase.db
You will see a prompt indicating that you are now inside the SQLite shell:
SQLite version 3.x.x
Enter ".help" for usage hints.
sqlite>
Next, create a table named users
to store basic user information. This table will have three columns: id
(a unique identifier), name
, and balance
. Enter the following SQL command at the sqlite>
prompt and press Enter:
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
balance REAL
);
This command sets up the users
table where:
id
is an integer that serves as the primary key for each user.
name
is a text field representing the user's name.
balance
is a real number representing the user's account balance.
Now, insert some initial data into the users
table:
INSERT INTO users (name, balance) VALUES ('Alice', 100.0);
INSERT INTO users (name, balance) VALUES ('Bob', 200.0);
These commands add two users, Alice and Bob, with initial balances of 100.0 and 200.0, respectively.
To confirm that the data was added correctly, run this command to view all records in the table:
SELECT * FROM users;
Expected Output:
1|Alice|100.0
2|Bob|200.0
This output shows the id
, name
, and balance
for each record. The SELECT *
command retrieves all columns from the specified table.