Why does SQLalchemy throw an operational error when I try to delete a row using engine.connect().execute()?

28 views Asked by At

I am working on a project that relies heavily on sql. In the python script I edit the database via a pandas dataframe, and then translate those changes to the database. When doing that I must delete the old rows and add the new ones.

def close():
    with engine.connect() as conn:
        with Session(engine) as session:
            careerList = []
            for i in range(len(df["NAME"])):
                conn.execute(text(f"DELETE FROM career WHERE name={df['NAME'][i]}")) ##throws an error
                careerList.append(Career(name=df["NAME"][i], university=df["UNIVERSITY"][i], cut_grade=df["CUT GRADE"][i]))
            session.add_all(careerList)
            session.commit()

when deleting a career named "Bellas Artes" the script failed with this error message: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "Artes": syntax error [SQL: DELETE FROM carrera WHERE nombre=Bellas Artes] (Background on this error at: https://sqlalche.me/e/20/e3q8)

I've searched the syntax and documentation of SQL commands, but apparently it's alright. I then checked df["NOMBRE"][i], but it returns the correct string. I'm also pretty new to SQL, so I don't really know what to do in these cases. Could someone explain why does this error appear?

0

There are 0 answers