Question

In: Computer Science

Given is a Python program that connects to a sqlite database and has one table called...

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

What is output. Getting nothing but errors with existing help. Again, this is for Python3

Solutions

Expert Solution


As per the problem statement I have solve the problem. Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)


Code:
//main.py

# Prompt user to enter the title for the dat user going to enter and store
dataTitle = input("Please Enter a title for the data: ")
print("YUser entered:", dataTitle)

# Prompt user to enter the first column name
column1 = input("\nPlease Enter the column 1 header: ")
print("User entered:", column1)

# Prompt user to enter the second column name
column2 = input("\nPlease Enter the column 2 header: ")
print("User entered:", column2)

inputMessage = "\nEnter a data point (-1 to stop inserting data and print the data): "
# Create data list
dataList = []

# While loop to enter the data continuously
while True:
    # Prompt user to enter the data point
    dataPoint = input(inputMessage)

    if dataPoint != "-1":
        if "," in dataPoint:
            # split method to split the data point in two
            dataInfo = dataPoint.split(",")
            if len(dataInfo) > 2:
                print("Error: enter the correct from of datat.")
                inputMessage = "\nPlease Enter a valid data point: "
            elif not dataInfo[1].replace(" ", "").isdigit():
                print("Error: Please enter the data in correct form.")
                inputMessage = "\nEnter a valid data point: "
            else:
                # Output the stinf and integer from the data
                print("Data string:", dataInfo[0])
                print("Data integer:", dataInfo[1].replace(" ", ""))
                inputMessage = "\nEnter a data point (-1 to stop inserting data and print the data): "
                # Add enter data point to the end of the list
                dataList.extend(dataInfo)
        elif "," not in dataPoint:
            print("Error: No comma in string.")
            inputMessage = "\nEnter a valid data point: "
    else:
        break

# print the data title and the both column enter by the user
print("\n%33s" % dataTitle)
print("%-20s|%23s" % (column1, column2))
print("-" * 44)

# iterate data throught the list
index = 0
while index < len(dataList):
    print("%-20s|%23s" % (dataList[index], dataList[index + 1]))
    index += 2

# print the histogram of the data enter by the user
i = 0
print("")
# while loop to print the all the data points
while i < len(dataList):
    print("%s %s" % (dataList[i], (int(dataList[i + 1]) * "*")))
    i += 2



--------------------------------------------------------------------------------------------------------------------------------------------------
Program Code screenshot:


--------------------------------------------------------------------------------------------------------------------------------------------------

Program Output Screenshot:


Related Solutions

In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to...
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to a sqlite database. Create a table called Horses with the following fields: id (integer): a primary key and not null name (text) breed (text) height (real) birthday (text) Next, insert the following data row into the Horses table: id: 1 name: 'Babe' breed: 'Quarter Horse' height: 15.3 birthday: '2015-02-10' Output all records from the Horses table. Ex: With the above row inserted, the output...
Write a Python program, in a file called StickFigure.py, which, given a value for the total...
Write a Python program, in a file called StickFigure.py, which, given a value for the total height of a stick figure, uses a recursive function to generate the stick figure pattern as shown below. The recursive function is used to print the entire figure. Note that the head, arms and legs stay the same, but the body can be different lengths, depending on what the user enters as the height of the figure. The "body" length is always 3 lines...
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the...
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the Canvas exam instructions. Name your output file “output.txt”. Ensure the program consists of a main function and at least one other function. Ensure the program will read the input file, and will output the following information to the output file as well as printing it to the screen: output the text of the file to screen output the number of words in the file...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result:             25   69   51   26 171...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result: 25 69 51 26 171 68 35...
Create a web interface that has a backend database that takes a value of one table...
Create a web interface that has a backend database that takes a value of one table and displays it match from another table. Create a web interface, that has a backend database, the database contains two tables, one table contains courses in one curriculum, while the other table in the database has corresponding classes from another curriculum. A user should be able to pick a course from the curriculum, and the matching course from the other table should populate on...
Using Python, write a segment of code to populate the table "employee" of the database "EmployeeDB”...
Using Python, write a segment of code to populate the table "employee" of the database "EmployeeDB” with the data below. Import your choice of DB connector (import MySQLdb/sqlite3…) Create the“employee” table with schema = [name, address, age] Insert this employee: John Doe, 7001 E Williams Field, 32
magine a table is given as following. This table is called lookup table. The table works...
magine a table is given as following. This table is called lookup table. The table works as following. It assigns a number to some uppercase or lowercase alphabents as shown in the table. A B F M N P Q W Z letter number letter Number 12 e 8    10 f 3    5 i 2    4 m 0    0 n 1    11 p 2    10 q 3    9 r 4    9 s...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
Python using sqllite3 package In this exercise, your task is to inspect the given database, which...
Python using sqllite3 package In this exercise, your task is to inspect the given database, which is called 'chinook.db', as you can see from the testing code below in the example. We first would like to know how the logical schema of the database looks like, in order to work with it later in terms of reading from and writing to the database. Please also note that a software tool like "DB Browser for SQLite" can be used to inspect...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT