Question

Using python and SQLite, connect to the database called "world.db". a. Input the data into table...

Using python and SQLite, connect to the database called "world.db".

a. Input the data into table 'city' and print

b. Using python, print out the country Name, Continent, Region and the country's Population of all the countries, whose country population is larger than 1,000,000,000. Print out all the fields for the cities in the U.S. whose city population is larger than 1,000,000.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Full working python code:

#Firstly importing sqlite3 module from python library
import sqlite3
from sqlite3 import Error

def sqlite_connection():
    try:
        conn = sqlite3.connect('world.db') #Creates the database world.db in the system memory
        return conn                        #if the connection made then return the connection
    except Error:
        print(Error)                      #if connection failed to make then throw error
        
def sqlite_table(conn):
    #cursor is method of connection object to execute SQlite statements in Python
    cursorObj = conn.cursor()
    #execute method executes the sqlite commands
    cursorObj.execute(
        "CREATE TABLE city(id integer PRIMARY KEY, country_name text, continent text, region text, population real)")
    #commit method is used to save changed done in the database with execute statement
    conn.commit()

#method to insert rows into the table
def sqlite_insert(conn, row):
    #again define the cursor. Everytime we need to excute sql query we need to define cursor for that
    cursorObj = conn.cursor()
    #? represent that it is expected to get its value from tuple row
    cursorObj.execute(
        'INSERT INTO city(id, country_name, continent, region, population) VALUES(?, ?, ?, ?, ?)', row)
    conn.commit()

#method to select or display desired rows for question part 1
def sqlite_select1(conn):
    cursorObj = conn.cursor()
    #Countries with population > 1000000000 will be returned only
    cursorObj.execute('SELECT country_name, continent, region, population FROM city WHERE population > 1000000000')
    #Fetch all method is uded to get all the values from the cursor and storing it into a variable
    rows = cursorObj.fetchall()
    #Now iterate through that variable to display the desired rows
    for row in rows:
        print(row)
        
#function to get all records for question part 2. Same as above just the select query is being changed
def sqlite_select2(conn):
    cursorObj = conn.cursor()
    cursorObj.execute('SELECT * FROM city WHERE continent = "United States" AND population > 1000000')
    rows = cursorObj.fetchall()
    for row in rows:
        print(row)

conn = sqlite_connection()  #Try to make connection with sqlite
sqlite_table(conn)          #If connection made the create sqlite table with that connection
row1 = (1, 'India', 'Asia', 'Peninsular', 30000000000)    #Define row 1 
sqlite_insert(conn,row1)    #Insert row 1
row2 = (2, 'Australia', 'Australia', 'Vascular', 20000000)
sqlite_insert(conn,row2)    #Insert row 2
row3 = (3, 'Chicago', 'United States', 'A', 2000000)
sqlite_insert(conn,row3)    #Insert row 3
row4 = (4, 'New Mexico', 'United States', 'B', 3400000)
sqlite_insert(conn,row4)    #Insert row 4
row5 = (5, 'Washington', 'United States', 'C', 100000)
sqlite_insert(conn,row5)    #Insert row 5
row6 = (6, 'Sri Lanka', 'Asia', 'Peninsular', 200000)
sqlite_insert(conn,row6)    #Insert row 6
row7 = (7, 'Russia', 'Russia', 'X', 35000000)
sqlite_insert(conn,row7)    #Insert row 7
print('Solution 1:')
sqlite_select1(conn)        #Displays the data for question part 1
print('Solution 2:')
sqlite_select2(conn)        #Displays records for part 2 question

Screenshots;

Output:

Directory with .py file and database file:

Add a comment
Know the answer?
Add Answer to:
Using python and SQLite, connect to the database called "world.db". a. Input the data into table...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • if you do not have the world schema you can obtain the scripts to create the...

    if you do not have the world schema you can obtain the scripts to create the database schema from https://dev.mysql.com/doc/index-other.html Queries Write a SQL query that shows all the columns in the country table for the North America region. The results need to be sorted in ascending order by population. Write a SQL query that shows the unique regions in the country table except for the regions from the continent of Oceania. The results need to be sorted in ascending...

  • The Northwind database created by Microsoft contains the sales data for a fictitious company called Northwind...

    The Northwind database created by Microsoft contains the sales data for a fictitious company called Northwind Traders, which imports and exports specialty foods from around the world. Here is the schema of the database: Products (ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued); Suppliers (SupplierID, CompanyName, ContactName , ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax, HomePage); Categories (CategoryID, CategoryName, Description, Picture); Orders (OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia,  Freight, ShipName,ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry); Order_details (ID, OrderID,...

  • *In Python please***** This program will display some statistics, a table and a histogram of a...

    *In Python please***** This program will display some statistics, a table and a histogram of a set of cities and the population of each city. You will ask the user for all of the information. Using what you learned about incremental development, consider the following approach to create your program: Prompt the user for information about the table. First, ask for the title of this data set by prompting the user for a title for data, and then output the...

  • Please refer to the following business scenarios: (i) Using SELECT statements of SQL, find the employee...

    Please refer to the following business scenarios: (i) Using SELECT statements of SQL, find the employee id, first name, last name, job title and email of all employees working in Asia (i.e. all countries coming from the region     ‘Asia’). This query must be implemented as a nested query! (ii) Using SELECT statements of SQL, find the employee id, first name, last name, job title and supervisor id of employees who had worked for more than 3 years and completed...

  • 23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4...

    23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same...

  • Hi. Please help me solve this in python using only while loops, if statements. Can't use...

    Hi. Please help me solve this in python using only while loops, if statements. Can't use for loops and lists. In this programming assignment, you will be writing a program that prints out several shapes via text. In this PA, you will be required to use functions and parameters. This is both to reduce redundancy and to make your code clean and well-structured. Name your program shaper.py Your program should have the capability to print out three different shapes: An...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this...

    23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same as what you had for your chapter 13 labs). The following is an example of...

  • Infinite Spiral of Numbers (due 17 Feb 2020) HELLO, WE ARE USING PYTHON 3 TO COMPLETE...

    Infinite Spiral of Numbers (due 17 Feb 2020) HELLO, WE ARE USING PYTHON 3 TO COMPLETE THIS PROJECT!! PLEASE FOLLOW CODE SKELETON AS GIVEN AT THE END. THIS IS DUE 17TH FEB 2020, ANY AND ALL HELP WOULD BE GREATLY APPRECIATED, THANK YOU! Consider the natural numbers laid out in a square spiral, with 1 occupying the center of the spiral. The central 11 x 11 subset of that spiral is shown in the table below. 111 112 113 114...

  • Read about Cokes strategy in Africa in the article below and discuss the ethics of selling...

    Read about Cokes strategy in Africa in the article below and discuss the ethics of selling soft drinks to very poor people. Is this an issue that a company like Coke should consider? Africa: Coke's Last Frontier Sales are flat in developed countries. For Coke to keep growing, Africa is it By Duane Stanford Piles of trash are burning outside the Mamakamau Shop in Uthiru, a suburb of Nairobi, Kenya. Sewage trickles by in an open trench. Across the street,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT