0

I have sqlite select query and can't see what is going wrong.

team_id, season_type, season, game_date = 'some_id', '2014-15', 'Some Season', '2015-00-00T00:00:00'
cur.execute('SELECT teams_stats_in_game.rebounds FROM teams_stats_in_game '
 'INNER JOIN games'
 'ON games.id = teams_stats_in_game.game_id '
 'AND games.home_team_id = teams_stats_in_game.team_id '
 'WHERE games.home_team_id = %s '
 'AND games.season_type = %s '
 'AND games.season = %s '
 'AND games.date_est < %s '
 'ORDER BY games.date_est DESC' % (team_id, season_type, season, game_date))

Trace:

Traceback (most recent call last):
'ORDER BY games.date_est DESC' % (team_id, season_type, season, game_date))
OperationalError: near "Season": syntax error
asked Feb 24, 2016 at 14:49
2
  • 2
    You are missing space after the INNER JOIN games . Commented Feb 24, 2016 at 14:52
  • @sagi thanks, but there is another mistake Commented Feb 24, 2016 at 14:56

1 Answer 1

3

@sagi has a good point. I would though recommend using a multi-line string for the query:

cur.execute("""
 SELECT 
 teams_stats_in_game.rebounds 
 FROM 
 teams_stats_in_game 
 INNER JOIN games 
 ON games.id = teams_stats_in_game.game_id AND 
 games.home_team_id = teams_stats_in_game.team_id
 WHERE games.home_team_id = ? AND 
 games.season_type = ? AND 
 games.season = ? AND 
 games.date_est < ? 
 ORDER BY 
 games.date_est DESC""", (team_id, season_type, season, game_date))

Also note that I've replaced %s with ? placeholders and now passing the query parameters in the second argument to execute() which makes this query parameterized.

answered Feb 24, 2016 at 14:55
Sign up to request clarification or add additional context in comments.

2 Comments

I believe that % is the wild card in SQLite, and looking at the query it would make more sense to write it as: (‘SELECT teams_stats_in_game.rebounds FROM teams_stats_in_game INNER JOIN games ON games.id = teams_stats_in_game.game_id AND games.home_team_id = teams_stats_in_game.team_id WHERE games.home_team_id = ? AND games.season_type = ? AND games.season = ? AND games.date_est < ? ORDER BY games.date_est DESC""" ?’, [team_id, season_type, season, game_date])
@Tbizzness great point. Completely agree, we need a parameterized query, updated - thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.