Question

In: Computer Science

In this assignment, you will write a Python script to increase the population of each city...

In this assignment, you will write a Python script to increase the population of each city in the world_x database by 10% (rounded).

First, add a new column to the "world_x" city table using the following MySQL command:

  • ALTER TABLE `world_x`.`city` ADD COLUMN `Population` DOUBLE NULL DEFAULT NULL AFTER `Info`;

The existing population data are stored as JSON datatype in the city table in a field named Info. You learned about JSON data types in Module 2. To obtain the population data from the Info column in the city table, run the following MySQL command:

  • select info-'$.Population' from city

Your Python script should do the following:

  • Copy the population data from the "Info" column into the "Population" column,
  • Increase the population by 10% in the "Population" column, and
  • List the population before the increase and the population after it was increased.

Submit your Python script and Python script output as two separate text files included in a zip file. Submit your zip file to the Module 6 folder. Provide a detailed technical report on the steps needed to integrate Python and MySQL. In addition, provide details on how you iterate through a JSON field in a Python scrip

Solutions

Expert Solution

For indundation %

To answer the above question, I am dividing it into three parts :
Python script, which does the above mentioned tasks
Steps needed to integrate Python and MySQL
Iterating through a json field in a python script
Now elaborating every point one by one:
1.Python script, which does the above mentioned tasks Assumptions :
MySQL DB is set up on the local DB and you know the credentials.
MySQL db has a DB named "SampleDB" with a table named which has fields of (json type) and population (of INT type)
info field looks something like {'population':500}
Python script:
import mysql.connector mydb=mysql.connector.connect(host='localhost',database='Sampledb', user='root', password ='123456') ;
mycursor = mydb.cursor()
copy data from info colulmn to population column mycursor.execute("update city set population = JSON_EXTRACT(info, '$.population')") ;

mydb.commit()
Population before increase
print('Population before increase')
mycursor.execute("SELECT city_id,population FROM city") myresult = mycursor.fetchall()
for x in myresult:
city_id=x[0]
population=x[1]
print(city_id,population)
Increase the population by 10% in the Population column mycursor.execute("update city set population = population + population *0.10")

mydb.commit()
Population after increase
print('Population after increase')
mycursor.execute("SELECT city_id,population FROM city") myresult = mycursor.fetchall()
for x in myresult:
city_id=x[0]
population=x[1]
print(city_id,population)
2.Steps needed to integrate Python and MySQL
Installation on machine : Python 2.7 and MySQL should be installed on the system
Python packages : mysql-connector-python [using pip install MySQL-connector-python]

Step 1:
import mysql.connector
Step 2 :
Make MySQL DB connection using mydb=mysql.connector.connect(host='Iocalhost',database='SampleD This will validate that the credentials and other DB information is corr Step 3:
Execeute queries (like we did in the above part)
3.Iterating through a json field in a python script For iterating a json mysql field in python, we use
JSON_EXTRACT(info, '.population')
where info is the name of the column and population is one of the feel extract the value of any field in json.
Another approach would have been to extract whole json data from a json module.
Example :
import json
mycursor.execute("SELECT info FROM city") myresult = mycursor.fetchall()
for x in myresult:
json_info=json.loads(x)
print(json_info['population'])


Related Solutions

Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
Write a Python script that: Ask the customer for their current bank balance. One thing you...
Write a Python script that: Ask the customer for their current bank balance. One thing you dont have to worry about is whether or not the user enters a negative or positive amount.. Ask the customer if they wish to deposit ('d') or withdraw ('w') from that amount. Ask them to enter the amount of money they are either depositing or withdrawing. This has to be a positive numeric value. If it is negative, you should print an error message...
#python. Explain code script of each part using comments for the reader. The code script must...
#python. Explain code script of each part using comments for the reader. The code script must work without any errors or bugs. Ball moves in a 2D coordinate system beginning from the original point (0,0). The ball can move upwards, downwards, left and right using x and y coordinates. Code must ask the user for the destination (x2, y2) coordinate point by using the input x2 and y2 and the known current location, code can use the euclidean distance formula...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT