0% found this document useful (0 votes)
22 views

Lab 02 Exercises

The document provides instructions for a lab exercise to extend a Python program that connects to a MySQL database. The program is to be extended to allow the user to select a database and view its tables, select a table and view its contents, and execute custom queries. Connectivity to the database requires establishing an SSH tunnel from off-campus networks. Sample output is provided.

Uploaded by

lijames800
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lab 02 Exercises

The document provides instructions for a lab exercise to extend a Python program that connects to a MySQL database. The program is to be extended to allow the user to select a database and view its tables, select a table and view its contents, and execute custom queries. Connectivity to the database requires establishing an SSH tunnel from off-campus networks. Sample output is provided.

Uploaded by

lijames800
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

COMPSCI 751 Lab 02 Exercises 1

Computer COMPSCI 751 S1 C – Lab 02


Science

10 marks in total = 1% of the final grade

1. In Lab01 question 2, we used the PhpMyAdmin tool as an interface for accessing the
MySQL DBMS back end. In this question, we use a programming interface to directly access
the created databases of the MySQL DBMS.
MySQL Connector for Python (https://dev.mysql.com/doc/connector-python/en/) provides a
database connectivity mechanism for communicating with MySQL servers through a Python
program. Please read the above page link and install the ‘mysql.connector’ on your
Python3 runtime environment. The following is a simplified code segment that enables the
login to your MySQL account and displays the names of all databases.

import mysql.connector
from getpass import getpass
from mysql.connector import connect, Error

def dbexec(conn, query):


with conn.cursor() as cursor:
cursor.execute(query)
for tb in cursor:
print(tb)

def main():
try:
with connect(
host="fosmysqlprd01.its.auckland.ac.nz",
user=input("Enter username: "),
password=getpass("Enter password: "),
) as connection:
print('DATABASE NAMES')
dbexec(connection, "SHOW DATABASES")

# extend your code here

except Error as e:
print(e)

main()

Your task is extending the above program in order to achieve the additional functions below:
• Allow the user to select a database and display its tables. The DML statements are
‘USE database_name' and 'SHOW TABLES'. Note that database_name should
be replaced with an actual database name.
• Allow the user to select a table and display its content. The DML statement is
'SELECT * FROM table_name'. Note that table_name should be replaced with
an actual table name.
• Allow the user to enter a DML query statement and display the result of the query.

You can download the “Lab02_Q1_code.py” file and extend it.


2 COMPSCI 751 Lab 02 Exercises

Notes on connectivity to the database: Because the MySQL database server is installed
within the UOA intranet, a secured connection is required to establish connectivity to the
database. Here are three possible options:

• Computers inside the University Lab (i.e., LAN connection):


Directly run the Lab 02 code. After verifying the database login credentials, connectivity
can be established.

• Laptops using the University WiFi network (e.g., UOA WiFi):


(a) Follow the instructions in the enclosed document titled ‘Off-campus MySQL
server access’ to set up a PuTTY session. After the connection has been established
with PuTTY, you can run the code on your local machine through the PuTTY tunnel (you
need to change the host address in the code to '127.0.0.1').

(b) For MacOS and Linux users, please run the following command in a terminal:

ssh -L 3306:fosmysqlprd01.its.auckland.ac.nz:3306 [email protected]

Remember to replace 'your_upi' in the command above with your own UPI. After the SSH
tunneling has been established, you can run the code on your local machine through the
SSH tunnel (you need to change the host address in the code to '127.0.0.1').

Note that you do not need to copy the code onto the server login account. You just need to
keep the server login window active in the background and run the code from your local
machine.

• Network outside the University:


Establish a VPN-secured connection to the UOA network first, then use option 2 above to
establish the database connectivity.

A sample output of the completed program is shown as follows. Note that ‘abcd001’ is used
as the example login UPI. You should replace it with your own student UPI value.

Enter username: abcd001


Enter password:
DATABASE NAMES
('information_schema',)
('performance_schema',)
('teaching_',)
('teaching_abcd001_A1',)
('teaching_abcd001_COMPANY',)
('teaching_abcd001_UNIVERSITY',)
('teaching_abcd001_Lab01',)
Select a database: teaching_abcd001_lab01
('COURSE',)
('GRADE_REPORT',)
('PREREQUISITE',)
('SECTION',)
('STUDENT',)
Select a table to show content: SECTION
(85, 'MATH2410', 'Fall', '07', 'King')
(92, 'CS1310', 'Fall', '07', 'Anderson')
(102, 'CS3320', 'Spring', '08', 'Knuth')
(112, 'MATH2410', 'Fall', '08', 'Chang')
COMPSCI 751 Lab 02 Exercises 3

(119, 'CS1310', 'Fall', '08', 'Anderson')


(135, 'CS3380', 'Fall', '08', 'Stone')
Enter a query statement: SELECT Course_name, Course_number,
Credit_hours FROM COURSE WHERE Department='CS'
('Intro to Computer Science', 'CS1310', 4)
('Data Structures', 'CS3320', 4)
('Database', 'CS3380', 3)
[5 marks]

2. Consider the following ER diagram for part of a BANK database and answer the
questions below. Note that each bank can have multiple branches, and each branch can
have multiple accounts and loans.

a. List the strong (non-weak) entity types in the ER diagram.


b. Is there a weak entity type? If so, give its name, partial key, and identifying
relationship.
c. What constraints do the partial key and the identifying relationship of the weak
entity type specified in this diagram?
d. List the names of all relationship types, and specify the (min, max) constraint on
each participation of an entity type in a relationship type.
e. List concisely the user data requirements that led to this ER design. State
appropriate assumptions to make the specification complete.
[5 marks]

You might also like