Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

sqlite - selecting from a list in SQL and python

I need help with the following scenario:

in my database file I have a table who looks something like this

https://i.stack.imgur.com/dCMwJ.png

I want to get a list of all the IDs that have suppliers_id=(2,3) (list of ids)

the problem is that I don't know which values are in the group (I'm getting the list in the function header from other SELECT function)

We were taught that we can use IN option that should get me the result I want but I cant getting it to work

my code (doesn't work for now):

    def update(self, suppliers_id):
        c = self._conn.cursor()
        c.execute("""SELECT id FROM Vaccines WHERE supplier_id IN (?)""", [suppliers_id])

I'm getting sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type. error

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to select multiple values from table, you've to add it as a tuple without assigning it to a new memory address .

By assigning your variable as a list, you're assigning it to a new memory address which is insufficient way of using Parameters Substitution.

Code Syntax

def update(self, suppliers_id):
    c = self._conn.cursor()
    c.execute("""SELECT id FROM Vaccines WHERE supplier_id IN (?)""", supplier_id, )   # by adding only comma, you're accessing the tuple, iteratively.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...