1

I'm trying to run a python function on the cursor.execute parameter but it just throws me this error. I'm using psycopg2

Traceback (most recent call last):
File "cliente.py", line 55, in <module>
cursorDB.execute(get_datos_animal('falsa'))
psycopg2.errors.UndefinedColumn: column "falsa" does not exist
LINE 1: ...e, clasificacion FROM animales WHERE animales.hierro = falsa

and my python function is this one

def get_datos_animal(hierro_v):
return "SELECT hierro, registro, nombre, fecha_nacimiento, raza, sexo, hierro_madre, hierro_padre, clasificacion FROM animales WHERE animales.hierro = " + str(hierro_v)

any idea what i ́m doing wrong?

Have several functions like this with same errors.

Hemant
1,4382 gold badges12 silver badges21 bronze badges
asked May 9, 2020 at 5:08

2 Answers 2

1

Use the automatic parameter quoting provided by your connection to ensure that values in queries are always quoted correctly, and to avoid SQL injection attacks.

stmt = """SELECT hierro, registro, nombre, fecha_nacimiento, raza, sexo, hierro_madre, hierro_padre, clasificacion 
 FROM animales 
 WHERE animales.hierro = %s"""
cursor.execute(stmt, (hierro_v,))
answered May 9, 2020 at 11:39
Sign up to request clarification or add additional context in comments.

Comments

0

In postgres if you pass value without quotes it will treat that as column name.

Try this:

def get_datos_animal(hierro_v):
return "SELECT hierro, registro, nombre, fecha_nacimiento, raza, sexo, hierro_madre, hierro_padre, clasificacion FROM animales WHERE animales.hierro = '"+str(hierro_v)+"'"
answered May 9, 2020 at 7:50

Comments

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.