Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
1598 Vistas
Hello, I am trying to execute this code from a server action but it is returning an error and I can't find how to solve it.

for record in records:

  if record.x_disponibilidad:   

    import psicopg2

    host = "localhost"

    port = "5432"

    user = "postgres"

    pas = "postgres"

    db = "SAG070524"

        

    connection_conf = """ 

        host=%s user=%s password=%s dbname=%s 

        """ % (host,port,pas,db)

        

    connection = psicopg2.connect(connection_conf)

    cursor = connection.cursor()

      

    query="update x_reg_asignaciones set x_sec = 1"

    cursor.execute(query)

    cursor.close()

    connection.close()

This is the error that is returning me

forbidden opcode(s) in 'for record in records:\n if record.x_disponibilidad: \n import psicopg2\n host = "localhost"\n port = "5432"\n user = "postgres"\n pas = "postgres"\n db = "SAG070524"\n \n connection_conf = """ \n host=%s user=%s password=%s dbname=%s \n """ % (host,port,pas,db)\n \n connection = psicopg2.connect(connection_conf)\n cursor = connection.cursor()\n \n query="update x_reg_asignaciones set x_sec = 1"\n cursor.execute(query)\n cursor.close()\n connection.close()': IMPORT_NAME


Avatar
Descartar
Mejor respuesta

Hi,

The error message is related to the IMPORT_NAME opcode. It seems like you’re trying to import the psycopg2 module inside a loop, which is generally not recommended and could be causing this error. Also please check the spelling of the import statement.


Refer to the code below:


import psycopg2


for record in records:

    if record.x_disponibilidad:

        host = "localhost"

        port = "5432"

        user = "postgres"

        pas = "postgres"

        db = "SAG070524"


        connection_conf = """

        host=%s user=%s password=%s dbname=%s

        """ % (host,port,pas,db)


        connection = psycopg2.connect(connection_conf)

        cursor = connection.cursor()


        query="update x_reg_asignaciones set x_sec = 1"

        cursor.execute(query)

        cursor.close()

        connection.close()


Hope it helps

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
0
feb 24
1369
2
jul 22
4952
2
mar 21
2711
1
mar 21
18413
0
dic 23
1311