Lost connexion to server MySQL after trying to enter arguments of a function as a record in a table

Viewed 39

I am still learning MySQL, and Query languages in general. I decided to work on a project in order to practice my skills but I seem to have an issue to not be able to insert data into my table in MySQL through a function.

I first connect to the database and create cursor as usual:

import mysql.connector


#connecting to the database
conn= mysql.connector.connect (

    host="localhost",
    user="root",
    password="Password1",
    database="project_yassine"
    )


#creating the cursor
mycursor= conn.cursor()

Then I created a function to add record into the table

def add_employee(firstName, lastName, CIN, Salary, PHONENUMBER, FIELD):

    data_employee= (firstName, lastName,CIN, Salary, PHONENUMBER, FIELD)

    #query inserting into the table

    sql = """ INSERT INTO employee (first_name, last_name, cin, salary, phone_number, field_work)

        VALUES( %s, %s, %s, %s, %s, %s)"""
    mycursor.execute(sql, data_employee)
    print("Record Added")

When in another file I run the following:

import employee

if __name__ == "__main__":
    employee.add_employee("Hamza", "Slaoui", "BJ440627", 5000, "0665838604", "CS")

I get the following error_message

mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 10038 An operation was attempted on something that is not a socket

Please also see the screenshot for more details.

I checked previous posts about this specific issue in the website and increased the maximum allowed packet (as one of the solutions I found). Hence I guess I have a syntax issue but I'm not sure what exactly.

My personal guess is that I couldn't transmit the arguments of the function into a suitable format for the query sequence but I am not sure how to do it (or my guess is wrong).

All help are highly appreciated.

Thank you very much all.

0 Answers
Related