Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The code below works as is . Your job is to expand the code and add some new features. You will also expand the given
The code below works as is Your job is to expand the code and add some new features. You will also expand the given test code. There are five specific tasks for you to complete. Each task is shown in a comment in the code. After you have completed all the tasks, submit the finished, improved code as a py file to this Canvas Dropbox.
import sqlite
import csv
conn sqliteconnecttestdb
print
Database connected."
##TASK : Add one more column to the table. Be sure to adjust all queries to reflect this extra column!
#CREATE A TABLE
conn sqliteconnecttestdb
conn.executeDROP TABLE IF EXISTS CONTACTS'
conn.executeCREATE TABLE CONTACTS
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR;
printTable created."
conn.close
##TASK : Create an additional test record.
# ADD RECORDS
conn sqliteconnecttestdb
conn.executeINSERT OR IGNORE INTO CONTACTS IDNAME, AGE,ADDRESS
VALUES 'Polly', 'Seattle';
conn.executeINSERT or IGNORE INTO CONTACTS IDNAME, AGE,ADDRESS
VALUES 'Roger', 'Bainbridge';
conn.commit
printRecords created.
conn.close
#RETRIEVE RECORDS
conn sqliteconnecttestdb
cursor conn.executeSELECT from CONTACTS"
printRECORDS:
for row in cursor:
print ID strrow
print NAME row
printAGE strrow
print ADDRESS strrow
conn.close
##TASK : Generalize the query so the user can input a column name.
#USER QUERY FROM CLI
query inputEnter a query term. Must be the ID
#column input TODO
conn sqliteconnecttestdb
#Task Place the query below into a "try: except:" block. The exception should say 'Query not valid'.
cursor conn.executeSELECT from CONTACTS where ID query
print
SELECTED RECORD:"
for row in cursor:
print ID strrow
print NAME row
printAGE strrow
print ADDRESS strrow
conn.close
#SAVE RECORDS TO A LIST
conn sqliteconnecttestdb
cursor conn.executeSELECT from CONTACTS"
datalist
for row in cursor:
datalist.appendrow
conn.close
printRECORDS LIST:"
printdatalist
#Task Let the user enter the name of the CSV file for data export.
#Include the selected file name in the final print statement.
#SAVE RECORDS TO A CSV FILE
with opencontactfile.csv modew as contactfile:
contactwriter csvwritercontactfile, delimiter quotechar quotingcsvQUOTEMINIMAL
for row in datalist:
contactwriter.writerowrow
#Print statement should reflect the userselected file name.
print
CSV EXPORT COMPLETE"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started