Count specific information in .db file - python

I wish to count the number of males and females in a specific city that the user chose. Here's a gist of the .db file:
example_table
CODE AGE SEX CITY
---- --- --- ----
E101 25 M New York
E102 42 F New York
E103 31 M Chicago
E104 67 F Chicago
This is what I've coded so far based on the references I've read:
city=input("Input city: ")
import sqlite3
db = sqlite3.connect('covid.db')
cursor = db.cursor()
sql = 'SELECT Sex, COUNT(Code) FROM example_table GROUP BY Sex'
data = cursor.execute(sql).fetchall()
for sex, cases in data:
print(sex, ':', cases)
cursor.close()
So far, that prints the overall number of males and females in the data. I'd like to ask how could I print the exact number of males and females in a city? Say I input "New York", the result would be:
M : 1
F : 1

Add a WHERE clause restricting to a certain city, e.g.
sql = 'SELECT Sex, COUNT(Code) FROM example_table WHERE CITY = ? GROUP BY Sex'
params = ('New York',)
data = cursor.execute(sql, params).fetchall()
for sex, cases in data:
print(sex, ':', cases)

Related

Get a higher average population of cities that fall into a certain country name

I have a case problem like this (pandasql):
Find cities in the United States (uscity) that meet all of the
following conditions:
has the name 'city' which is not the same as 'state_name', and also
has a population above the average population of the cities in 'county_name' Miami-Dade, and also
consists of two or more words (example: Los Angeles, New York)
The expected output is only the 'city' column, no need for other
columns
Do only 1x query, don't do more than 1 query
query = """
SELECT DISTINCT city
FROM uscity
WHERE city != state_name
AND avg(population) > country_name = 'Miami-Dade'
AND city like '% %'
"""
sql_run(query)
However, after I run the code, I can't get the output.
Do you get an error message ?
Also the avg(population) > country_name = 'Miami-Dade' is incorrect.
You need a city which has a population above the average population of the cities in 'county_name' Miami-Dade. So population > avg(select population from uscity where country_name = 'Miami-Dade')
query = """
SELECT DISTINCT city
FROM uscity
WHERE city != state_name
AND population > avg(select population from uscity where country_name = 'Miami-Dade')
AND city like '% %'
"""
I slightly changed the answer you wrote
Maybe this will help because I've tried it and it works
query = """
SELECT DISTINCT city
FROM uscity
WHERE population > (SELECT avg(population)
FROM uscity
WHERE county_name = 'Miami-Dade')
AND city != state_name
AND city like '% %'
"""
sql_run(query)

How to insert MAX (text) as WHERE clause?

I should be searching based on two WHERE clauses at the same time. One is a simple clause, the other is MAX (text). How to solve?
x = cursor.execute('SELECT product FROM electronics WHERE brand = ? AND MAX(price)' [combo_brand])
Price is a column of a database. I want to take the highest value of the column records. I would like to select the product with the maximum price, and search by it and by brand at the same time.
I am getting this error:
TypeError: string indices must be integers
If you want to select the product having the max price for a given brand, then use:
sql = '''SELECT product
FROM electronics
WHERE brand = ? AND price = (SELECT MAX(e.price)
FROM electronics e
WHERE e.brand = electronics.brand)'''
x = cursor.execute(sql, (combo_brand,))
records = cursor.fetchall()
print("Total rows are: ", len(records))
for row in records:
print(row)
If instead you want the product with the maximum price across all brands, then use:
sql = '''SELECT product
FROM electronics
WHERE brand = ? AND price = (SELECT MAX(price) FROM electronics)'''
x = cursor.execute(sql, (combo_brand,))
But I suspect the first version is what you want.

How to update row by row using python function in sqlite3?

This question is duplicated I think, but I couldn't understand other answers...
Original table looks like this.
NAME AGE SMOKE
John 25 None
Alice 23 None
Ken 26 None
I will update SMOKE column in these rows,
But I need to use output of function which was coded in python check_smoke(). If I input name to check_smoke(), then it returns "Smoke" or "Not Smoke".
So final table would look like below:
NAME AGE SMOKE
John 25 Smoke
Alice 23 Not Smoke
Ken 26 Not Smoke
I'm using sqlite3 and python3.
How can I do it? Thank you for help!
You could use 1 cursor to select rows and another one to update them.
Assuming that the name of the table is smk (replace it by your actual name) and that con is an established connection to the database, you could do:
curs = con.cursor()
curs2 = con.cursor()
batch = 64 # size of a batch of records
curs.execute("SELECT DISTINCT name FROM smk")
while True:
names = curs.fetchmany(batch) # extract a bunch of rows
if len(names) == 0: break
curs2.executemany('UPDATE smk SET smoke=? WHERE name=?', # and update them
[(check_smoke(name[0]), name[0]) for name in names])
con.commit()

CSV and SQLITE PANDAS WITH DIFFERENT Columns and Values

I imported a csv file into Python and tried using SQLLite. But when I created the table, the columns and values were changed. For example, in the overall column the values "asin" appeared. The reviewerID column received the values "overall" and so on.
How to fix it?
class csvrd(object):
def csvFile(self):
self.readFile('reviews.csv')
def readFile(self, filename):
conn = sqlite3.connect('amazonReviews.db')
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS amazonReviews(reviewerID varchar, asin INT,reviewerName varchar,reviewText varchar, overall INT,summary varchar,unixReviewTime INTEGER,reviewTime INTEGER,helpful INT,total INT)""")
filename.encode('utf-8')
print("Amazon Reviews table executed")
with open(filename) as f:
reader = csv.reader(f)
for field in reader:
cur.execute("INSERT INTO amazonReviews VALUES (?,?,?,?,?,?,?,?,?,?);", field)
print("CSV Loaded into SQLite")
conn.commit()
conn.close()
c = csvrd().csvFile()
con = sqlite3.connect('amazonReviews.db')
pd.read_sql_query("SELECT * FROM amazonReviews LIMIT 5", con)
EXPECTED:
reviewerID asin reviewerName reviewText overall summary unixReviewTime reviewTime helpful total
0 A1EE2E3N7PW666 B000GFDAUG Aaron L. Allen "Orgazmo1009" CA Lewsi' review should be removed. he's revie... 5 Stupid 1202256000 02 6, 2008 0 0
1 AGZ8SM1BGK3CK B000GFDAUG Mind's Clay I truly love the humor of South Park. It's soc... 5 "More Moist Than Should Be" Humor 1198195200 12 21, 2007 1 1
ACTUAL:
asin overall reviewText reviewTime reviewerID reviewerName summary unixReviewTime helpful total
0 A1EE2E3N7PW666 B000GFDAUG Aaron L. Allen "Orgazmo1009" CA Lewsi' review should be removed. he's revie... 5 Stupid 1202256000 02 6, 2008 0 0
1 AGZ8SM1BGK3CK B000GFDAUG Mind's Clay I truly love the humor of South Park. It's soc... 5 "More Moist Than Should Be" Humor 1198195200 12 21, 2007 1 1[enter image description here][1]
This would happen if the table already exists with the columns in the "ACTUAL" result order. Table will not be created because of "IF NOT EXISTS" in the "CREATE" sql. The data will be loaded without "type" complaint because of manifest typing.

SQL selecting multiple MAX and MIN - Python

I have a program which stores scores for students within a database. I want to select the max score for each student. At the moment, only one student with the highest score out of all students is returned, and not the highest score for each student. Can anyone help me out with this ? (FYI there is one "score" column which has a list of scores stored in it)
def ClassSort():
myFile = sqlite3.connect("scores.db")
c = myFile.cursor()
clear()
classNo = input("Enter the class number you would like to sort... ")
clear()
type = input("How would you like to sort ? \n 1 - Highest score, with students in alphabetical order \n 2 - Highest score, highest to lowest \n 3 - Average score, highest to lowest \n... ")
if type == "1":
c.execute('SELECT MIN(score), name FROM scores WHERE classNo = (?)', (classNo,))
if type == "2":
c.execute('SELECT MAX(score), name FROM scores WHERE classNo = (?)', (classNo,))
if type == "3":
c.execute('SELECT AVG(score), name FROM scores WHERE classNo = (?)', (classNo,))
row = c.fetchall()
row = ', '.join(map(str, row))
print(row)
myFile.commit()
myFile.close()
You need to add a GROUP BY statement. Aggregation functions in sql are usually most useful with a GROUP BY.
SELECT MAX(score), name FROM scores WHERE classNo = (?) GROUP BY name

Categories

Resources