Thursday, February 7, 2019

Building your world - part two: creating your database and the first room

Creating the database

This project will be created using PythonAnywhere, if you are using another development environment, you may need to change the process.

So, once you are in your PythonAnywhere console, click on the Databases tab.

Scroll down to the Create Database option.
Type in the new database name, in this case: "adventure", and click on Create.

The new database will be added to the list under Your Databases.

Make sure you have not got two existing consoles (the limit on the free version). If you do, then you will need to go the Consoles tab and close one.

Click on the option to start a console on <your user name>$adventure.

The database is empty, you can check by entering the command show tables; at the command prompt. This will return an empty set message.

Creating your first table

The first table we require is rooms.
This will have the following fields:
  • an id number (an integer which will be auto incremented  so we do not need to set a value).
  • A name (up to twenty characters)
  • A description (up to one hundred characters)
At the command prompt type:
create table rooms( 
     roomID INT NOT NULL AUTO_INCREMENT,
     name VARCHAR(20) NOT NULL,
     description VARCHAR(100) NOT NULL,
     PRIMARY KEY(roomID)
);
Press return and it should add the table.

You can check by entering the command show tables; at the command prompt. 
mysql> show tables;+--------------------------------------+| Tables_in_technologyisnotd$adventure |+--------------------------------------+| rooms |+--------------------------------------+1 row in set (0.00 sec)

Of course there is nothing in there at the moment:
mysql> select * from rooms;Empty set (0.00 sec)

Populating the first room

To add some data to the table, enter the following at the command prompt:

insert into rooms(name,description) values('Kitchen','A bright shiny IKEA kitchen');

Now when you select from the table, you will see the first room.
mysql> select * from rooms;+--------+---------+-----------------------------+| roomID | name | description |+--------+---------+-----------------------------+| 1 | Kitchen | A bright shiny IKEA kitchen |+--------+---------+-----------------------------+1 row in set (0.01 sec)