M
M
mshuribitov2021-08-12 17:28:32
Python
mshuribitov, 2021-08-12 17:28:32

How to get all the values ​​of a column into a list from a database?

Hello, you need to get all the values ​​of one column from the database.
Let's say there are many users and you need to take their ID from each and write them into one list, how to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-12
@mshuribitov

If you use the sqlite3 engine to work with the database, then it will be something like this

con = sqlite3.connect("mydb.sqlite") #подразумеваю, что таблицу ты уже создал
cur = con.cursor()
cur.execute("SELECT id FROM users")
ids = [row['id'] for row in cur]

Only if there is a lot of data, such a request can consume a lot of memory.
So a counter question: why do you need to select the entire table? Maybe you still need to filter some of the records?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question