Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print row that includes key with the smallest value

this is another question about my cities program. I am trying to print the city with the smallest population. I got it to print the smallest population, but I would like it to print the entire row with that population. Here is my code:

import sqlite3
import os 

# Remove Database file if it exists:

os.remove('cities.db')

connection = sqlite3.connect("cities.db")
cursor = connection.cursor()

cursor.execute("create table if not exists cities(city_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, cities_name TEXT, city_population INTEGER)")
    
cities_list = [('Minneapolis', 425336),
    ('St. Paul', 307193),
    ('Dallas', 1288000),
    ('Memphis', 628127),
    ('San Francisco', 815201),
    ('Milwaukee', 569330),
    ('Denver', 711463),
    ('Phoenix', 1625000),
    ('Chicago', 2697000),
    ('New York', 8468000)]

cursor.executemany("insert into cities(cities_name, city_population) values (?, ?)", cities_list)
connection.commit()

# Print entire table: 
for row in cursor.execute("select * from cities"):
    print(row)

# Print cities in alphabetical order:

cursor.execute("select cities_name from cities")
result = sorted(cursor.fetchall())
print(result)

# Print average:

cursor.execute("select city_population from cities")
result = list(cursor.fetchall())
average = sum(list(map(sum, list(result)))) / len(result)
print(average)

# Print city with the smallest population:

cursor.execute("select city_population from cities")
result = list(cursor.fetchall())
result.sort()
print('Smallest population is: ', result[0])

connection.commit()
connection.close()  

I tried using the output of the smallest population and plugging it into some code the prints a row based that includes a specified value. Unfortunately, it didn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can get sqlite to sort the cities for you.
Replace

cursor.execute("select city_population from cities")

with

cursor.execute("select cities_name, city_population from cities order by city_population asc limit 1")

After that, the result variable should contain the name and population of the city with the smallest population.

You also don’t need the result.sort() line after this change, as the sorting is now happening on the database side.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading