简体   繁体   中英

PostgreSQL INSERT based on SELECT results using Python

I have been trying to mess around with this issue for the past week and have not been able to work around it. I have a PostgreSQL database which keeps track of players playing matches in a tournament. I am working on a function which reports the results of matches. The way it reports the results of matches is by simply updating the database (I'm not worrying about the actual reporting system at the moment).

Here is the function which does the reporting:

def reportMatch(winner, loser):
"""Records the outcome of a single match between two players.

Args:
  winner:  the id number of the player who won
  loser:  the id number of the player who lost
"""
connection = connect()
cursor = connection.cursor()
match_played = 1

insert_statement = "INSERT INTO matches (winner_id, loser_id) VALUES (%s, %s)"
cursor.execute(insert_statement, (winner, loser))
cursor.execute("INSERT INTO players (match_count) VALUES (%s) SELECT players.id FROM players where (id = winner)" (match_played,)) # here is where I have my issue at runtime
connection.commit()
cursor.execute("INSERT INTO players(match_count) SELECT (id) FROM players VALUES (%s, %s)",(match_played, loser,))
connection.commit()
connection.close

The line which is I have commented out above is where I get an error. To be more precise and pinpoint it out: cursor.execute("INSERT INTO players(match_count) VALUES (%s) SELECT (id) FROM players WHERE (id = %s)",(match_played, winner,))

The error given is the following:

File "/vagrant/tournament/tournament.py", line 103, in reportMatch cursor.execute(insert_statement_two, (match_played, winner)) psycopg2.ProgrammingError: syntax error at or near "SELECT" LINE 1: INSERT INTO players(match_count) VALUES (1) SELECT (id) FROM...

If it helps, here is my schema:

CREATE TABLE players (
  id serial PRIMARY KEY,
  name varchar(50),
  match_count int DEFAULT 0,
  wins int DEFAULT 0,
  losses int DEFAULT 0,
  bye_count int
);

CREATE TABLE matches (
  winner_id serial references players(id),
  loser_id serial references players(id),
  did_battle BOOLEAN DEFAULT FALSE,
  match_id serial PRIMARY KEY
);

I have some experience with MySQL databases, but am fairly new to PostgreSQL. I spent a lot of time poking around with guides and tutorials online but haven't had the best of luck. Any help would be appreciated!

You can't both specify VALUES and use SELECT in an INSERT statement. It's one or the other.

See the definition of INSERT in the Postgres doc for more details.

You also do not need to specify the id value, as serial is a special sequence.

That line is also lacking a comma after the INSERT string.

In order to specify particular values, you can narrow down the SELECT with a WHERE , and/or leverage RETURNING from the earlier INSERT s, but the specifics of that will depend on exactly how you want to link them together. (I'm not quite sure exactly what you're going for in terms of linking the tables together from the code above.)

I suspect using RETURNING to get 2 IDs from players that were INSERT ed and using those in turn in the INSERT into matches is along the lines of what you're looking to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM