Getting Error while running MySql Query Run command: Incorrect syntax near '`'

Hi Team,

this is my query:

“UPDATE RAN_Plan_activity set Daily_WO = '”+Update_Daily_WO+“', Daily_WO_Creation_Date&Time = '”+Update_Daily_WO_Creation_DateTime+“’ Where Site ID='”+Site_ID+“’ and PMR Planned Date ='”+CurrentDate+“';”

anyone please

thanks
shaik

Hello,

You might need to install one using pip :

Define your update values

Update_Daily_WO = “some_value”
Update_Daily_WO_Creation_DateTime = “2023-10-01 12:00:00”
Site_ID = “site_id_example”
CurrentDate = “2023-10-01”

Prepare the SQL query using parameter substitution

query = “”"
UPDATE RAN_Plan_activity
SET Daily_WO = %s, Daily_WO_Creation_Date&Time = %s
WHERE Site ID = %s AND PMR Planned Date = %s;
“”"

try:
# Create a cursor object
with connection.cursor() as cursor:
# Execute the SQL command
cursor.execute(query, (Update_Daily_WO, Update_Daily_WO_Creation_DateTime, Site_ID, CurrentDate))

# Commit the transaction
connection.commit()

except Exception as e:
print(f"An error occurred: {e}")

finally:
# Close the connection
connection.close()

By using placeholders like %s and passing the values separately, you protect against SQL injection.

Use try...except blocks to catch any errors during execution.

Remember to commit the transaction if the operation involves modifying the database.

Always close the database connection after your operations are complete to free up resources.

Thanks

@shaik.muktharvalli1,

Try this one:

"UPDATE RAN_Plan_activity 
 SET Daily_WO = '" + Update_Daily_WO + "', 
     Daily_WO_Creation_Date_Time = '" + Update_Daily_WO_Creation_DateTime + "' 
 WHERE Site_ID = '" + Site_ID + "' 
     AND PMR_Planned_Date = '" + CurrentDate + "';"

Also check what’s the datatype of Update_Daily_WO_Creation_DateTime and what’s expected in SQL table as well.

@ashokkarale

Daily_WO
Daily_WO_Creation_Date&Time
Site ID
PMR Planned Date

These are all columns coming from Database, we can’t change them

Thanks
Shaik

@shaik.muktharvalli1,

With the column names you provided, try this one

"UPDATE RAN_Plan_activity 
 SET Daily_WO = '" + Update_Daily_WO + "', 
     Daily_WO_Creation_Date_Time = '" + Update_Daily_WO_Creation_DateTime + "' 
 WHERE [Site ID] = '" + Site_ID + "' 
     AND [PMR Planned Date] = '" + CurrentDate + "';"

@ashokkarale

In my database data contains Daily_WO_Creation_Date&Time column not Daily_WO_Creation_Date_Time

Can share how to handle this

Thanks
Shaik

@shaik.muktharvalli1,

I think you would be able to handle this by passing like this

[Daily_WO_Creation_Date&Time]

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.