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

Question 2 [2 points] Write Python code that reads from cars.db and produces a l

ID: 3903129 • Letter: Q

Question

Question 2 [2 points] Write Python code that reads from cars.db and produces a list, cars, which contains the number of German passenger cars sold in the US The table was created using the following SQL command: CREATE TABLE carsales (year INTEGER, sales INTEGER) The values were then inserted (in chronological order) as: INSERT INTO carsales (year, sales) VALUES (1990, 171900) INSERT INTO carsales (year, sales) VALUES (1991, 150000) And so on (up to the year 2009) In [ ]: | # YOUR CODE HERE raise NotimplementedError) In assert carsto 1719000 In [ ]: | # Hidden test case for Question 2

Explanation / Answer

#!/usr/bin/python import MySQLdb # Connect db = MySQLdb.connect(host="localhost", user="appuser", passwd="", db="cars.db") cursor = db.cursor() # Execute SQL select statement cursor.execute("SELECT sales FROM carsales order by year DESC") # Get the number of rows in the resultset numrows = cursor.rowcount cars = [] # Get and display one row at a time for x in range(numrows): row = cursor.fetchone() cars.append(int(row[0])) # Close the connection db.close() assert cars[0] == 171900

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote