Given is a Python program that connects to a sqlite database and has one table called writers with two columnns: 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) here is what is I got: import csv, sqlite3 from sqlite3 import Error def insert_writer(con,data): c=con.cursor()#creating a cursor object to execute the query query="INSERT INTO writers VALUES {}".format('')#creating the query to insert writer into database c.execute(query)#executing the query using the execute() method con.commit()#commiting(saving) the changes into the database def update_writer(con,writer): c=con.cursor()#creating a cursor object to execute the query #creating the query to update the writer in the database num=int(input("14")) query="UPDATE writers SET num={'')".format('num,writer') c.execute(query)#executing the query con.commit()#commiting(saving) the changes into the database def check_writer(con,name): c=con.cursor()#creating a cursor object to execute the query #creating a query to check if the writer exists or not query="SELECT EXISTS(SELECT * FROM writers WHERE name='')".format('name') c.execute(query)#executing the query res=c.fetchone()#fetching the result of the query and saving it to the variable res return res()#returning the value(i.e 0 if writer does not exist and 1 if the writer exist) #You only need asked for the code upto here con = sqlite3.connect('Writer.db')#creating a connection to the database while True:#loop to add or update entries choice=input(""); if choice=='y'or choice=='Y': name=input(" ") if(check_writer(con,name)==1): print("This writer already exist.") update_writer(con,name) elseif print("This writer does not exist in the database. \nDo you want to create one?(y/n): ") choice1=input() if choice1=='y' or choice1=='Y': num=int(input("Enter the number of books this writer has written: ")) data=(name,num) insert_writer(con,data) elseif c=con.cursor()#creating a cursor object query="SELECT * FROM writers"#creating the query to fetch the data from the table c.execute(query)#executing the query res=c.fetchall()#fetching all values from the result of the query print("Table contents after adding and updating writers:\n") print("(ID,Name,Num)") for i in range(len(res)): print("({},{},{})".format(i+1,res[i][0],res[i][1])) con.close()#closing the connection

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Given is a Python program that connects to a sqlite database and has one table called writers with two columnns:

  • 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)

here is what is I got:

import csv, sqlite3
from sqlite3 import Error

def insert_writer(con,data):
c=con.cursor()#creating a cursor object to execute the query
query="INSERT INTO writers VALUES {}".format('')#creating the query to insert writer into database
c.execute(query)#executing the query using the execute() method
con.commit()#commiting(saving) the changes into the database

def update_writer(con,writer):
c=con.cursor()#creating a cursor object to execute the query
#creating the query to update the writer in the database
num=int(input("14"))
query="UPDATE writers SET num={'')".format('num,writer')
c.execute(query)#executing the query
con.commit()#commiting(saving) the changes into the database

def check_writer(con,name):
c=con.cursor()#creating a cursor object to execute the query
#creating a query to check if the writer exists or not
query="SELECT EXISTS(SELECT * FROM writers WHERE name='')".format('name')
c.execute(query)#executing the query
res=c.fetchone()#fetching the result of the query and saving it to the variable res

return res()#returning the value(i.e 0 if writer does not exist and 1 if the writer exist)

#You only need asked for the code upto here

con = sqlite3.connect('Writer.db')#creating a connection to the database
while True:#loop to add or update entries
choice=input("");
if choice=='y'or choice=='Y':
name=input(" ")
if(check_writer(con,name)==1):
print("This writer already exist.")
update_writer(con,name)
elseif
print("This writer does not exist in the database. \nDo you want to create one?(y/n): ")
choice1=input()
if choice1=='y' or choice1=='Y':
num=int(input("Enter the number of books this writer has written: "))
data=(name,num)
insert_writer(con,data)
elseif

c=con.cursor()#creating a cursor object
query="SELECT * FROM writers"#creating the query to fetch the data from the table
c.execute(query)#executing the query
res=c.fetchall()#fetching all values from the result of the query
print("Table contents after adding and updating writers:\n")
print("(ID,Name,Num)")
for i in range(len(res)):
print("({},{},{})".format(i+1,res[i][0],res[i][1]))
con.close()#closing the connection

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education