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);
share|improve this question

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

c.execute("insert into Players values (%s);", (name,))
share|improve this answer
    
I knew I was missing something obvious. Thanks! – Rebecca Tauber Oct 12 '15 at 14:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.