Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I know there is some syntax error but not able to find it out. I made sure that it is getting insert in the right table.

column_update = input("Enter the column name where Data has to be updated: ")
update_data = input("Enter the new value: ")
column_name = input("Enter the column to get the row: ")
row_value = input("Enter the row value: ")

try:
    sql_update_query = """ UPDATE EmployeeList SET %s = %s WHERE %s = %s """
    my_cursor.execute(sql_update_query,params = (column_update,update_data,column_name,row_value))
    mydb.commit()
    print("Record Updated Successfully")

except mysql.connector.Error as error:
    print("Failed to update record to database: {}".format(error))

finally:
    if (mydb.is_connected()):
        my_cursor.close()
        mydb.close()
        print("MySQL connection is closed")

my table:

my_cursor.execute("CREATE TABLE IF NOT EXISTS EmployeeList(user_id INT AUTO_INCREMENT PRIMARY KEY,EMPID INT, Emp_Name VARCHAR(100),Designation VARCHAR(100), Role VARCHAR(100), Updated_by VARCHAR(100), LastUpdate TIMESTAMP DEFAULT NOW())")

user input details has to be given after running the script.

Enter the column name where Data has to be updated: Role
Enter the new value: Software
Enter the column name to get the row: EMPID
Enter the column value: 1

Error:

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 ''Role' = 'Software' WHERE 'EMPID' = '1'' at line 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
140 views
Welcome To Ask or Share your Answers For Others

1 Answer

sql_update_query = """ UPDATE EmployeeList SET %s = %s WHERE %s = %s;"""

Add a semicolon(;) at last and check once if it worked.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...