Answered step by step
Verified Expert Solution
Link Copied!

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 sqlite3
import csv
conn = sqlite3.connect("test.db")
print("
Database connected.")
##TASK 1: Add one more column to the table. Be sure to adjust all queries to reflect this extra column!
#CREATE A TABLE
conn = sqlite3.connect("test.db")
conn.execute('''DROP TABLE IF EXISTS CONTACTS''')
conn.execute('''CREATE TABLE CONTACTS
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50));''')
print("Table created.")
conn.close()
##TASK 2: Create an additional test record.
# ADD RECORDS
conn = sqlite3.connect("test.db")
conn.execute("INSERT OR IGNORE INTO CONTACTS (ID,NAME, AGE,ADDRESS)\
VALUES (10, 'Polly', 32, 'Seattle')");
conn.execute("INSERT or IGNORE INTO CONTACTS (ID,NAME, AGE,ADDRESS)\
VALUES (11, 'Roger', 28, 'Bainbridge')");
conn.commit()
print("Records created.
")
conn.close()
#RETRIEVE RECORDS
conn = sqlite3.connect("test.db")
cursor = conn.execute("SELECT * from CONTACTS")
print("RECORDS:")
for row in cursor:
print ("ID ="+ str(row[0]))
print ("NAME ="+ row[1])
print("AGE ="+ str(row[2]))
print ("ADDRESS ="+ str(row[3])+"
")
conn.close()
##TASK 3: Generalize the query so the user can input a column name.
#USER QUERY FROM CLI
query = input("Enter a query term. Must be the ID.")
#column = input (TODO)
conn = sqlite3.connect("test.db")
#Task 4- Place the query below into a "try: except:" block. The exception should say 'Query not valid'.
cursor = conn.execute("SELECT * from CONTACTS where ID ="+"'"+ query +"'")
print("
SELECTED RECORD:")
for row in cursor:
print ("ID ="+ str(row[0]))
print ("NAME ="+ row[1])
print("AGE ="+ str(row[2]))
print ("ADDRESS ="+ str(row[3])+"
")
conn.close()
#SAVE RECORDS TO A LIST
conn = sqlite3.connect('test.db')
cursor = conn.execute("SELECT * from CONTACTS")
data_list =[]
for row in cursor:
data_list.append(row)
conn.close()
print("RECORDS LIST:")
print(data_list)
#Task 5- 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 open('contact_file.csv', mode='w') as contact_file:
contact_writer = csv.writer(contact_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in data_list:
contact_writer.writerow(row)
#Print statement should reflect the user-selected file name.
print("
CSV EXPORT COMPLETE")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions

Question

KEY QUESTION Refer to Figure 3.6, page

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago