Python Forum

Full Version: Error When Writing to SQL Table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im getting an error "the machine refuses it" when running my script to write to a table
from pylogix import PLC
import sys
sys.path.append('..')
import time
import pymysql

with PLC() as comm:
    comm.IPAddress = '172.21.12.10'
    comm.ProcessorSlot = 0
    ret = comm.GetPLCTime()
    print( "Fecha y Hora de Registro Revision Fallas", ret.Value)
    time.sleep(1)
    Estacion = "D2R_030"
    print("Connecting To Database")

    connection = pymysql.connect(host='ACTWSLT01',
                             user='METALSA\satmslt',
                             password='T$m@l19*',
                             db='Python_PLC')
    cursor = connection.cursor()
    print("Connected")

#------------------------------------------------------------------------------------------ ESTACION D2R_030 -------------------------------------------------------------------------------#
    print("INICIO DE REVISION ESTACION :" , Estacion)
#ACTUADOR 1 
    ACTUADOR = comm.Read('D2R_030.Status.Act._1.WBypassed')
    ret = comm.GetPLCTime()
    Numero_Act_EnBypass = 0
    
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 1 WORK SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        sql = "INSERT INTO 'PLC Actuadores' ('Id', 'IP', 'Date', 'Estacion', 'Tag', 'Value') VALUES (%s, %s, %s, %s, %s)"
        cursor.execute(sql,(comm.IPAddress, ret.Value, Estacion, ACTUADOR, Sensor))
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False) 
        print(Sensor)
        connection.commit()

    else:
        Sensor = "ACTUADOR 1 WORK OK" 
        print(Sensor)

    comm._closeConnection
    connection.close()
    print("Terminado")
Error:
Traceback (most recent call last): File "C:\Users\jsalass\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 581, in connect sock = socket.create_connection( File "C:\Program Files (x86)\Python38-32\lib\socket.py", line 808, in create_connection raise err File "C:\Program Files (x86)\Python38-32\lib\socket.py", line 796, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\jsalass\Desktop\Prueba SQL.py", line 23, in <module> connection = pymysql.connect(host='ACTWSLT01', File "C:\Users\jsalass\AppData\Roaming\Python\Python38\site-packages\pymysql\__init__.py", line 94, in Connect return Connection(*args, **kwargs) File "C:\Users\jsalass\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 325, in __init__ self.connect() File "C:\Users\jsalass\AppData\Roaming\Python\Python38\site-packages\pymysql\connections.py", line 630, in connect raise exc pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'ACTWSLT01' ([WinError 10061] No connection could be made because the target machine actively refused it)")
It's saying the python script tried to connect to the mysql server and wasn't able to. Could be access restrictions or firewalls.