Can I add more than one value for the?

Question:

def StatDatabase(self, x, team, position):
    # Fetches data from the FifaStats database
    # I need to add more than one variable for the different cards
    self.cur.execute("SELECT " + x + " FROM Stats WHERE Club =? AND Card =? AND Position =?",
                     (f'{team}', 'ut21  gold rare', f'{position}'))
    data = self.cur.fetchall()
    if len(data) > 1:
        updated_data = random.sample(data, 1)
        for updated_info in updated_data:
            return updated_info
    else:
        for info in data:
            return info

Is there a way I can represent many values that I am looking for in the ? variable. For an example where it says ‘WHERE Club =? AND Card =?’, can I use ‘Gold’ and ‘Silver in the ‘Card’ record. How would I represent this?

Asked By: Kelan Westwood

||

Answers:

You can add OR condition in where clause, as follows,

def StatDatabase(self, x, team, position):
    # Fetches data from the FifaStats database
    # I need to add more than one variable for the different cards
    self.cur.execute("SELECT " + x + " FROM Stats WHERE Club=? AND (Card='Gold' OR Card='Silver') AND Position=?",
                     (f'{team}', 'ut21  gold rare', f'{position}'))
    data = self.cur.fetchall()
    if len(data) > 1:
        updated_data = random.sample(data, 1)
        for updated_info in updated_data:
            return updated_info
    else:
        for info in data:
            return info

Like Card you can do same for other attributes.

EDIT:
The above query can be optimized as:

self.cur.execute("SELECT " + x + " FROM Stats WHERE Club=? AND Card IN ('Gold', 'Silver') AND Position=?",
                         (f'{team}', 'ut21  gold rare', f'{position}'))

If you want to exclude the results you can use NOT IN

Answered By: Pradeep Yenkuwale