Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Type code in PYTHON please. Also type it please TYPE it to avoid messy handwriti

ID: 3833484 • Letter: T

Question

Type code in PYTHON please. Also type it please TYPE it to avoid messy handwriting.

1. Practice with SQLite (or Python plus Database)

Create in Python – use the class discussed methods of Try, Except and Finally – a SQLite database. The database should be named “my_first.db”. It should have the following two tables. Make it so user can input data please.

English

a) First Function. Create Python code to create the above tables and populate them with data as indicated above. Use Parametrized queries. You can add more data to the tables than the minimum described above.

b) Second Function. In a separate Python function read the data in the two tables and compute and print the average age of the students in each course Major. Again use Try, Except, Finally Loops.

Majors id_course Name 4 Mathematics 5 Chemistry 6 Physics 7 Computer Science 8

English

Explanation / Answer

Here is the command through which you can create database in SQLite

BEGIN TRANSACTION;

/* Create a table called Majors */
CREATE TABLE Majors(id_course integer PRIMARY KEY ,Name text);

/* Create few records in this table */
INSERT INTO Majors VALUES
(4,'Mathematics'),
(5,'Chemistry'),
(6,'Physics'),
(7,'Computer Science'),
(8,'English');

/* Create a table called Students */
CREATE TABLE Students(Id integer PRIMARY KEY AUTOINCREMENT, First_Name text, Last_Name text,Age integer,
                        id_course integer ,
            FOREIGN KEY (id_course) REFERENCES Majors (id_course));

/* Create few records in this table */
INSERT INTO Students (First_Name,Last_Name,Age,id_course) VALUES
('Doe','John',45,5),
('Bob','Smith',23,7),
('Sally','Jones',22,7),
('Janet','Cooke',35,7),
('Arthur','Peck',22,5),
('Robert', 'James', 22,5);
COMMIT;

/* Display all the records from the table */
SELECT * FROM Majors;
SELECT * FROM Students;

==============================================

Now import databse and run into pythonSqlite window:

Here is the command, please go through it thoroughly:-

#!/usr/bin/python

import my_firstdb

database = "CODINGGROUND"
username = 'root'
password = 'root'

# Open database connection
db = my_firstdb.connect(unix_socket='/home/cg/mysql/mysql.sock', host='localhost', user=username, passwd=password, db=database )

# prepare a cursor object using cursor() method
cursor = db.cursor()

sql = "select * from Majors"

try:
cursor.execute(sql)
results = cursor.fetchall()
print "<table>"
for row in results:
id_course = row[0]
Name = row[1]
print "<tr><td>%d</td><td>%s</td></tr>"% (id, Name)

print "</table>"

===============================

sql = "select * from Students"
try:
cursor.execute(sql)
results = cursor.fetchall()
print "<table>"
for row in results:
id = row[0]
First_Name = row[1]
Last_Name = row[2]
Age = row[3]
id_course = row[4]
print "<tr><td>%d</td><td>%s</td><td>%s</td><td>%d</td><td>%d</td></tr>"% (id, First_Name, Last_Name, age, id_corse)

print "</table>"

except:
print "Error: unable to fecth data"

db.close()

Please run these command into your window and check out the output.