I am trying to update a MySQL table with dynamic column name and value. I m always getting a syntax Error when I m doing the update. I have similar INSERT and SELECT statements which work as expected. I have already tried different variants of the query to get the right syntax
cursor.execute("""UPDATE movies SET %s="%s" WHERE Rank_id = %s""",(column.replace("'",""),str(movie[key]).replace("'",""),rank))
or
query = """UPDATE movies SET %s="%s" WHERE Rank_id = %s"""
values = (column,movie[key],rank)
cursor.execute(query,(column,str(movie[key]).replace("'",""),rank))
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Title'="'fsdsd'" WHERE Rank_id = '1'' at line 1
I tried to execute the SQL command on DBeaver and this works
update movies set Title="djhdfghgfjhjfg" where Rank_id=1
What am I doing wrong here?
Code that works
query = "INSERT INTO movies(Title,Genre,Description,Director,Actors,Release_Year,Runtime_Minutes,Rating,Votes,Revenue_Millions,Metascore) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
values =(title, genre, description, director,actors, release_year,runtime_minut, rating,votes,revenue_millions,metascore)
cursor.execute(query,values)
query = "SELECT Title,Genre,Description FROM movies WHERE Rank_id = %s";
cursor.execute(query,(rank,))
Update
I am able to get it working when I hardcode the column name in the Query (Though not the desired solution)