Question: Write a Python program to query a MySQL database and print the query results. Details: Make a connection to the MySQL database with this connection

Write a Python program to query a MySQL database and print the query results. Details:

Make a connection to the MySQL database with this connection information:

 cnx = mysql.connector.connect(user='rlawrenc', password='test', host='cosc304.ok.ubc.ca', database='WorksOn') 

Write and execute a SQL query that returns the department number (dno), department name (dname), project number (pno), project name (pname), sum of hours worked on the project as well as the number of employees working on the project. Only include records where the budget is greater than $140,000. Order by sum of hours worked descending.

Print out your query results. Note that tabs ("\t") are used to line up the data in columns nicely.

To install the MySQL driver using Anaconda, run this command:

conda install -c https://conda.anaconda.org/anaconda mysql-connector-python

WorksOn Relational Database Schema

emp (eno CHAR(5), ename VARCHAR(30), bdate DATE, title CHAR(2), salary DECIMAL(9,2), supereno CHAR(5), dno CHAR(5), PRIMARY KEY (eno)) dept (dno CHAR(5), dname VARCHAR(40), mgreno CHAR(5), PRIMARY KEY (dno)) -- mgreno is the employee number of the manager of the department (may join with emp) proj (pno CHAR(5), pname VARCHAR(40), budget DECIMAL(9,2), dno CHAR(5), PRIMARY KEY (pno)) -- dno is the department that has the project (may join with dept) workson (eno CHAR(5), pno CHAR(5), resp VARCHAR(20), hours SMALLINT, PRIMARY KEY (eno,pno)) -- Use eno to join with emp. Use pno to join with proj.

Write a Python program to query a MySQL database and print the

Sample Output dno dname hours numemp pno P4 pname Maimtenance nsultingz Budget P3 P1 D3 Accounting 462 2 D1 Management 36

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!