Home > Software design >  How do I use OR and AND statements together on sqlite3?
How do I use OR and AND statements together on sqlite3?

Time:08-06

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 Position =? AND Card =? Or Card =?",
                     (f'{team}', f'{position}', 'ut21  gold rare', 'ut21  gold non-rare'))
    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

When I execute this command, it does not recognise the fact that I want the players to be from a certain club and a certain position. In fact it retrieves most players from the database. How do I use OR statements and AND statements together?

CodePudding user response:

Try using parenthesis.

SELECT *
FROM table
WHERE (column_1 = 'a' AND column_2 = 'b') OR (column_1 = 'a' AND column_2 = 'c')
  • Related