Answer the question
In order to leave comments, you need to log in
How to display all data from a table using Python (SQLite)?
Hello everybody!
Relatively speaking, there is a database that looks like this:
ID product_name price shop
1 Bread 23 At home
2 Milk 50 Eight
3 Kefir 30 Petrovich
's I'm trying to write a function that will display ID and product_name
def get_all():
cur.execute('SELECT * FROM product_table')
rows = cur.fetchall()
for row in rows:
name = str(row[1])
id = str(row[0])
return name, id
return name, id
print(name,id)
Answer the question
In order to leave comments, you need to log in
then outputs all lines but the last line is None
print(get_all())
something def get_all():
records = []
cur.execute('SELECT * FROM stocks')
rows = cur.fetchall()
for row in rows:
records.append({'name': str(row[1]), 'id': str(row[0])})
return records
When return is called, the function stops and returns a value. return in a loop is a sin
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question