Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在postgres中执行python函数_Python_Postgresql - Fatal编程技术网

在postgres中执行python函数

在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

我试图在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 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,))