J
J
James_Obry2021-07-02 16:31:35
Python
James_Obry, 2021-07-02 16:31:35

How to use a loop to look at the entire database and pull out individual columns?

The bottom line is, I need to somehow check the entire database and pull out all the id's of records from it, where for example the name is Maxim and write this data to a variable, please tell me how to do this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
James_Obry, 2021-07-06
@James_Obry

It is worth saying that set() selects elements randomly, so if the task is also to build all the ids in order, you need to use: result.sorted()

with sq.connect("database.db") as con:
        cur = con.cursor()
        names = list(set(cur.execute("select id from table where name = 'Максим'").fetchall()))
        result= [x[0] for x in names]
        print(result)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question