Question

In: Computer Science

Write a program called popchg.py that enables DBVac to update the state populations for states in...

Write a program called popchg.py that enables DBVac to update the state populations for states in the databsae. Your program should: • Include a comment in the first line with your name. • Include comments describing each major section of code. • Connect to your personal version of the DBVac database. • Ask the user to input a two-letter state abbreviation. o 1 point Extra Credit: Validate that what the user entered was two characters long. If not, then have the user re-enter the abbreviation until it’s two characters long. • Ask the user to input the population of the new state. o 1 point Extra Credit: Validate that the value entered was an integer above 0. If not, keep asking the user to enter a value until they enter an integer above 0. • Update the database to change the population for the state entered to the new population value entered.

Solutions

Expert Solution

I have given the simplest possible code below with the necessary explanations in the comments followed by the #.

#import the mysql connector and system modules
#The code below assumes a MySQL database
#However it can easily modified for any other database

import mysql.connector
import sys

#Connect to the appropriate host and database
#Replace localhost with the host name or IP address,
#user with username used to connect to database
#password with the password used for database connection
#mention the database name accurately

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="dbVac"
)

#Capture the State Abbreviation from the user
#Initialize the variable first

inputState = ""

#Enter into a loop for validating the length of the input
#Validation can be accomplished in various ways
#Below is the simplest way however it can also be done
#with try and except statements as in the next code unit below

while len(inputState) != 2:
inputState = input("Enter two character State abbreviation: ")

#Capture the State population and validate the input
#Using the try and except block

while True:
inputPop = input("Enter the state population: ")
try:
value = int(inputPop)
if value > 0:
break
else:
print("Population should be greater than zero, try again")
except ValueError:
print("Population must be a number, try again")

#Create a cursor object of the database
mycursor = mydb.cursor()

#Capture the SQL statement used to update the Database as instructed
#Use the appropriate table name and column names in place of
#state_info, state_population and state_id (as per your personal database dbVac)
#Use %s to pass the variables captured through input
# This is to prevent SQL injection and is considered as a good practice
#You can read about this on Google

sql = "UPDATE state_info SET state_population = %s WHERE state_id = %s"
val = (inputState, inputPop)

#Execute the SQL statement and commit the query
#Print the results to confirm that the appropriate table is updated

mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")


Related Solutions

Write a program that utilizes a function called paymentCalc. In the main body of the program,...
Write a program that utilizes a function called paymentCalc. In the main body of the program, ask the user what is their principal and how many months they want the loan. If it is easier, ask the user for number of years, but don’t forget to change it to months for the calculation. Use a function to calculate their monthly mortgage payment. In the main body of the program, print out what their monthly mortgage payment will be. For simplicity,...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the method main. The program should ask for information from the user and then print it. Look at the examples below and write your program so it produces the same input/output. Examples (the input from the user is in bold face) % java AskInfoPrintInfo enter your name: jon doe enter your address (first line): 23 infinite loop lane enter your address (second line): los angeles,...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR...
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR PROGRAM INSTRUCTIONS PLEASE SEE BELOW) PROGRAM 3 Adjust program II to make use of functions. All the same rules from the previous program specifications still apply, for example input gathering, output formatting and breaking on -1 still apply. Additional requirements. Write a function that prompts the user for hours worked, rate and name. Use parameter passing, and pass by reference. Write a function that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT