sqlite 如何打印包含具有最小值的键的行

qyyhg6bp  于 2022-12-13  发布在  SQLite
关注(0)|答案(4)|浏览(124)

这是关于我的城市程序的另一个问题。2我试图打印人口最少的城市。3我让它打印人口最少的城市,但是我想让它打印人口最少的整行。4下面是我的代码:

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()

我尝试使用最小总体的输出,并将其插入到一些代码中,以打印包含指定值的行。不幸的是,它不起作用。

wlwcrazw

wlwcrazw1#

您可以让sqlite为您排序城市。替换

cursor.execute("select city_population from cities")

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

之后,result变量应包含人口最少的城市的名称和人口。
在此更改之后,您也不需要result.sort()行,因为排序现在在数据库端进行。

nhhxz33t

nhhxz33t2#

虽然用Python肯定可以做到这一点,但用SQL可能更好。
根据您提供的示例架构,以下查询应返回您要查找的内容:

SELECT * FROM cities
WHERE city_population = (
  SELECT MIN(city_population) FROM cities
)

查询会取得具有最小人口数的每一个数据列,并传回此值。这表示如果两个城市具有相同的最小人口数,查询会传回两者。
在python中,这看起来像cursor.execute("select * from cities WHERE city_population = ( SELECT MIN(city_population) FROM cities )")
下面是显示查询的SQLFiddle。

a11xaf1n

a11xaf1n3#

您可以让sql查询负责从数据库中检索已排序的数据,而不是使用python对列表进行排序。

  • 在删除数据库文件之前,还要添加检查条件。

main.py

import sqlite3
import os 

# Remove Database file if it exists:

if(os.path.isfile('cities.db')):
  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 city with the smallest population:

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

connection.commit()
connection.close()
7rfyedvj

7rfyedvj4#

你可以在sql中求解每一个数字,所以你不需要从数据库中提取那么多信息,这使得脚本更快

# Print average:
cursor.execute("select AVG(city_population) from cities")
result = cursor.fetchone()[0]
print(result)

# Print city with the smallest population:

cursor.execute("select city_population from cities ORDER BY city_population ASC LIMIT 1")
result = cursor.fetchone()[0]
print('Smallest population is: ', result)

相关问题