Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psycopg2.ProgrammingError: column "your name" does not exist

When I try to upload the PlayerScore with the code

if Number.PlayerScore > Score.interact_database("SELECT Score FROM score WHERE Name = %s;" % Player_Name, False,)[0][0]

to the PostgreSQL Database in Python I get this error: psycopg2.ProgrammingError: column "your name" does not exist , where 'your name' is the variable Player_Name. However, when I run it in the PostgreSQL Query Tool it works fine. Does anyone know why this errors pops up and how I can prevent it from happening again in the future?

like image 844
JasperMW Avatar asked Jun 14 '26 06:06

JasperMW


1 Answers

http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters

I think we'd need to see more of the code, but as per the docs:

Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint.

Try passing the parameters as the second argument in your cursor.execute() method. Using something like:

cursor.execute("""SELECT Score FROM score WHERE Name = %(player)s""", {'player': player_name } )
cursor.fetchone()

Should work. It can also accept a tuple of values.

like image 96
Devasta Avatar answered Jun 15 '26 20:06

Devasta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!