在postgres中执行python函数
我试图在cursor.execute参数上运行python函数,但它只是抛出了这个错误。 我用的是psycopg2在postgres中执行python函数,python,postgresql,Python,Postgresql,我试图在cursor.execute参数上运行python函数,但它只是抛出了这个错误。 我用的是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
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
知道我做错了什么吗
在postgres中,如果您传递的值不带引号,它将视为列名 试试这个:
def get_datos_animal(耶罗五世):
return“SELECT hierro,registro,nombre,fecha_nacimiento,raza,sexo,hierro_madre,hierro_padre,clasificacion FROM animales WHERE animales.hierro=”+str(hierro_v)+”
使用自动参数引号,以确保查询中的值始终被正确引用,并避免SQL注入攻击
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,))
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,))