LastName Address City Make Model Year Smith 123 Main Oak Park Ford Mustang 1972
ID: 662933 • Letter: L
Question
LastName Address City Make Model Year Smith 123 Main Oak Park Ford Mustang 1972 Jones 234 Pine LaGrange Dodge Charger 1968 Purnell 345 Maple Addison Studebaker Avanti 1975 Using module sqlite3 and SQL execute the following queries by encoding the appropriate Python statements. i. All the records for the city LaGrange. ii. All the records for city Oak Park and make Ford. iii. The LastName. Address and City for cars older than 1970. iv. The LastName. Make, Model and Year for all records in descending order of LastName.Explanation / Answer
============================================================
i. All the records for the city LaGrange
Ans:
----
SELECT * from Cars where city='LaGrange'
Program:
--------
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
cursor = conn.execute("SELECT * from Cars where city='LaGrange'")
for row in cursor:
print "LastName = ", row[0]
print "Address = ", row[1]
print "City = ", row[2]
print "Make = ", row[3]
print "Model = ", row[4]
print "Year = ", row[5], " "
print "Operation done successfully";
conn.close()
============================================================
ii. All the records for city Oak Park and make Ford.
Ans:
----
SELECT * from Cars where city='Oak Park' and make='Ford'
Program:
--------
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
cursor = conn.execute("SELECT * from Cars where city='Oak Park' and make='Ford'")
for row in cursor:
print "LastName = ", row[0]
print "Address = ", row[1]
print "City = ", row[2]
print "Make = ", row[3]
print "Model = ", row[4]
print "Year = ", row[5], " "
print "Operation done successfully";
conn.close()
============================================================
iii. The LastName, Address and City for cars older than 1970
Ans:
----
SELECT LastName, Address, City from Cars where Year<1970
Program:
--------
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
cursor = conn.execute("SELECT LastName, Address, City from Cars where Year<1970")
for row in cursor:
print "LastName = ", row[0]
print "Address = ", row[1]
print "City = ", row[2]
print "Operation done successfully";
conn.close()
============================================================
iv. The LastName, Make, Model and Year for all records in descending order of LastName.
Ans:
----
SELECT * from Cars ORDER BY LastName DESC;
Program:
--------
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
cursor = conn.execute("SELECT * from Cars ORDER BY LastName DESC")
for row in cursor:
print "LastName = ", row[0]
print "Address = ", row[1]
print "City = ", row[2]
print "Make = ", row[3]
print "Model = ", row[4]
print "Year = ", row[5], " "
print "Operation done successfully";
conn.close()
============================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.