J
J
James_Obry2022-01-14 21:39:17
Python
James_Obry, 2022-01-14 21:39:17

How to pull rows from database excluding some of them?

There is this function:

def funciton():
    list = [1, 3, 5]
    with sq.connect(database_path) as con:
        cur = con.cursor()
        id = cur.execute(f"select row from table where id != {list} order by random() limit 1").fetchone()
        print(id)


I need it to pull 1 random entry from the database, but if the id of this entry is in the list list, then skip this entry and select the one where the id is not equal to those recorded in the list list

Tell me if this is possible, and how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-01-14
@James_Obry

I'm not sure about sqlite, but sql (like python) has an in operator that checks if it's in a list. In your case it should be something like this.
where id not in (v1, v2... vn)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question