2

I am trying to do the code as follows to register a player with a given name, but I can't get the argument name to do anything… I thought that %s was the variable to insert a string into a database, but it doesn't seem to work.

import psycopg2

def registerPlayer(name):
    """Registers new player."""
    db = psycopg2.connect("dbname=tournament")
    c = db.cursor()
    c.execute("insert into Players values (%s);")
    db.commit()
    db.close()

registerPlayer("Butter")

When I run it, I get the error message:

ProgrammingError: syntax error at or near "%"    
LINE 1: insert into Players values (%s);
colidyre
  • 3,401
  • 11
  • 33
  • 47
R Jackson
  • 105
  • 2
  • 13

1 Answers1

3

You haven't actually passed the parameter into the execute method.

c.execute("insert into Players values (%s);", (name,))
Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842