
Given is a Python program that connects to a sqlite
- name - the name of a writer
- num - the number of works the writer has written
The writers table originally has the following data:
name, num Jane Austen,6 Charles Dickens,20 Ernest Hemingway,9 Jack Kerouac,22 F. Scott Fitzgerald,8 Mary Shelley,7 Charlotte Bronte,5 Mark Twain,11 Agatha Christie,73 Ian Flemming,14 J.K. Rowling,14 Stephen King,54 Oscar Wilde,1
Update the Python program to ask the user if they want to update entries or add new entries. If the name entered already exists in the writers table then the database record is updated, overwriting the original contents. If the name does not exist in the writers table, then add a new record with the writer's name and number of works. The following TODO sections must be completed.
- Check if a writer exists in the writers table
- If the writer exists in the table, locate an entry to be updated by writer's name and update the writer's value for num
- If the writer does not exist in the table, add a new entry in the writers table and provide the value for name and num
Ex: If the input is:
y J.K. Rowling 30 y Elton John y 62 n
The output is:
(ID, Name, Num) (1, 'Jane Austen', 6) (2, 'Charles Dickens', 20) (3, 'Ernest Hemingway', 9) (4, 'Jack Kerouac', 22) (5, 'F. Scott Fitzgerald', 8) (6, 'Mary Shelley', 7) (7, 'Charlotte Bronte', 5) (8, 'Mark Twain', 11) (9, 'Agatha Christie', 73) (10, 'Ian Flemming', 14) (11, 'J.K. Rowling', 30) (12, 'Stephen King', 54) (13, 'Oscar Wilde', 1) (14, 'Elton John', 62)
import csv, sqlite3
from sqlite3 import Error
# TODO: Implement this function
# inserts new writer data as a row in the database. Insert both name and number of works.
def insert_writer(con, data):
# TODO: Implement this function
# updates an existing writer record based on writer name
def update_writer(con, writer):
# TODO: Implement this function
# Check if the writer's name exists in the database
# Return the number of records found (should be 1 if writer exits, or 0 if the writer doesn't exist in table)
def check_writer(con, name):
# NOTE: Do not modify
# Creates a sqlite3 database connection (in memory)
def create_connection():
""" create a database connection to a SQLite database """
con = None
try:
con = sqlite3.connect(":memory:")
return con
except Error as e:
print(e)
return con
# NOTE: Do not modify
# Creates a table
def create_table(con, create_table_sql):
""" create a table from the create_table_sql statement
:param cnn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c = con.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
# NOTE: Do not modify
# Prints all rows of data from the writers table
def select_all_writers(con):
"""
Query all rows in the tasks table
:param con: the Connection object
:return:
"""
cursor = con.cursor()
cursor.execute("SELECT * FROM writers")
rows = cursor.fetchall()
for row in rows:
print(row)
# NOTE: Do not modify
# Initializes the database by adding all rows from the allWorks.csv file
def initialize_database(con):
cursor = con.cursor()
## create writers table
create_table_sql = """ CREATE TABLE writers (
id integer PRIMARY KEY NOT NULL,
name text,
num integer
); """
create_table(con, create_table_sql)
## Read contents of CSV file
with open('allWorks.csv', 'r') as fin: # `with` statement available in 2.5+
# csv.DictReader uses first line in file for column headings by default
dr = csv.DictReader(fin) # comma is default delimiter
to_db = [(i['name'], i['num']) for i in dr]
## Insert contents of CSV file into writers table
cursor.executemany("INSERT INTO writers (name, num) VALUES (?, ?);", to_db)
con.commit()
if __name__ == '__main__':
# TODO: Create a database connection
con = create_connection()
# Initialize database data
initialize_database(con)
print('Would you like to update a writer database record?')
yesNo = input()
if yesNo[0] == 'y' or yesNo[0] == 'Y':
# Loop until user is done updating records
while yesNo == 'y' or yesNo == 'Y':
print('Which entry would you like to update?')
name = input()
# TODO Check if writer exists in database
if ( ):
print('Enter a new value for ' + name)
num = input()
# TODO Update writer's value for number of works
else:
print("Entry not found. Add new entry?")
yesNo = input()
if yesNo[0] == 'y' or yesNo[0] == 'Y':
print('Number of works?')
num = input()
# TODO Insert new writer entry
print('Would you like to continue to update writers')
yesNo = input()
print("(ID, Name, Num)")
select_all_writers(con)
con.close()

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 2 images

- (1) Prompt the user for a title for data. Output the title. Ex: Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored (2) Prompt the user for the headers of two columns of a table. Output the column headers. Ex: Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels (3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they have finished entering data points. Output the data points. Store the string components of the data points in an ArrayList of strings. Store the integer components of the data points in a second ArrayList of integers.Ex: Enter a data point (-1 to stop input): Jane Austen, 6 Data string: Jane Austen Data integer: 6 (4) Perform error checking for the data point entries. If any…arrow_forwardplease write in C++.arrow_forwardC++ Write a program that will allow a user to enter students into a database. The student information should include full name, student ID, GPA and status. The program should allow delete any student based on student ID.arrow_forward
- /* Created with SQL Script Builder v.1.5 */ /* Type of SQL : SQL Server */ CREATE TABLE EMPLOYEE ( EMP_CODE int, EMP_TITLE varchar(4), EMP_LNAME varchar(15), EMP_FNAME varchar(15), EMP_INITIAL varchar(1), EMP_DOB datetime, JOB_CODE varchar(5), STORE_CODE int ); INSERT INTO EMPLOYEE VALUES('1','Mr.','Williamson','John','W','5/21/1964','SEC','3'); INSERT INTO EMPLOYEE VALUES('2','Ms.','Ratula','Nancy','','2/9/1969','MGR','2'); INSERT INTO EMPLOYEE VALUES('3','Ms.','Greenboro','Lottie','R','10/2/1961','GEN','4'); INSERT INTO EMPLOYEE VALUES('4','Mrs.','Rumpersfro','Jennie','S','6/1/1971','GEN','5'); INSERT INTO EMPLOYEE VALUES('5','Mr.','Smith','Robert','L','11/23/1959','GEN','3'); INSERT INTO EMPLOYEE VALUES('6','Mr.','Renselaer','Cary','A','12/25/1965','GEN','1'); INSERT INTO EMPLOYEE VALUES('7','Mr.','Ogallo','Roberto','S','7/31/1962','MGR','3'); INSERT INTO EMPLOYEE VALUES('8','Ms.','Johnsson','Elizabeth','I','9/10/1968','SEC','1'); INSERT INTO EMPLOYEE…arrow_forwardDifferentiate between functions that return a scalar, an aggregate, and a table.arrow_forward/* Created with SQL Script Builder v.1.5 */ /* Type of SQL : SQL Server */ CREATE TABLE EMPLOYEE ( EMP_CODE int, EMP_TITLE varchar(4), EMP_LNAME varchar(15), EMP_FNAME varchar(15), EMP_INITIAL varchar(1), EMP_DOB datetime, JOB_CODE varchar(5), STORE_CODE int ); INSERT INTO EMPLOYEE VALUES('1','Mr.','Williamson','John','W','5/21/1964','SEC','3'); INSERT INTO EMPLOYEE VALUES('2','Ms.','Ratula','Nancy','','2/9/1969','MGR','2'); INSERT INTO EMPLOYEE VALUES('3','Ms.','Greenboro','Lottie','R','10/2/1961','GEN','4'); INSERT INTO EMPLOYEE VALUES('4','Mrs.','Rumpersfro','Jennie','S','6/1/1971','GEN','5'); INSERT INTO EMPLOYEE VALUES('5','Mr.','Smith','Robert','L','11/23/1959','GEN','3'); INSERT INTO EMPLOYEE VALUES('6','Mr.','Renselaer','Cary','A','12/25/1965','GEN','1'); INSERT INTO EMPLOYEE VALUES('7','Mr.','Ogallo','Roberto','S','7/31/1962','MGR','3'); INSERT INTO EMPLOYEE VALUES('8','Ms.','Johnsson','Elizabeth','I','9/10/1968','SEC','1'); INSERT INTO EMPLOYEE…arrow_forward
- Company Dinner Lili works part-time in a restaurant near her campus as a waitress. One day, a boss from a company down the street decided to hold a company dinner in the restaurant she worked at. Before the dinner, the food is arranged in a sorted order A according to their type. Moments later, the manager of the restaurant confronted Lili and asks Q questions, each one asks how many foods of type Bi are there. Lili has asked you as her friend to help her count and answer as fast as possible. Format Input A single line with two integers N and Q denoting the number of food and the number of question respectively, followed by a line containing N elements denoting the type of the foods and another Q lines each containing a single integer denoting the number of occurrences of the type of food asked by the manager. Format Output Q lines each with a single integer denoting the number of occurence of Bi in A. Constraints • 1 ≤ N, Q ≤ 10^5 • 1 ≤ Ai, Bi ≤ 10^9 • Ai ≤ Aj for all 1 ≤ i ≤ j ≤ N…arrow_forwardBelow is “Book Order,” the only table in library management system’s database. The design of “Book Order” as you may tell is in the zero normal formal form, you as the database designer want to convert the design into the third normal form. Order ID Special order date Customer ID Customer last name Customer First name Customer birth date Book ISBN1 Book Title 1 Book Author 1 Book publication year 1 Book ISBN2 Book Title 2 Book Author 2 Book publication year 2 Store ID Store name Store location Special order status Book Order(Order ID, Special order date, Customer ID, Customer last name, Customer First name, Customer birth date, Book ISBN1, Book Title 1, Book Author 1, Book publication year 1, Book ISBN2, Book Title 2, Book Author 2, Book publication year 2, Store ID, Store name, Store location, Special order status)arrow_forwardGiven the following Python code below: co2_sensors = [{"loc_id":1,"cities":{"Quezon City": {"Commonwealth": {"temp":30,"co2_level":5}}},"networkgroup_id":"G01" },{"loc_id":2,"cities":{"Marikina City":{"Nangka": {"temp":37,"co2_level":2}},"Taguic City": {"Bagumbayan": {"temp":26,"co2_level":3}}},"networkgroup_id":"G02" }] what is the codee that print the co2_level level in barangay nangka in marikina City?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





