0

I am trying to insert html to mysql table using Python script. That way i want to edit article on my website, so the html has to be valid. This is an error i receive:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server version for the right syntax to use 
near 'n1'>Poniedziałek 30.08.2021r.</p>
<p>
        <b>6<sup>30</sup></b> za zmarłych ro...' at line 1

Perhaps there is a problem with special characters like /n, /t or '. Unfortunately I don't know how to solve that problem.

Here is part of code that I am trying to execute:

mycursor.execute("UPDATE `table_name` SET `col_name` = '" + doc_html + "' WHERE `table_name`.`id` = 1")

Beginning of doc_html (which is a string variable):

<html>\n<style>\n    p {\n        margin: 0em 0in;\n        font-size: 1em;\n        font-family: "Georgia", serif;\n        color: black;\n        text-align: left;\n    }\n\n    p.n1 {\n        margin: 1.5em 0in 0.25em;\n        font-size: 1.25em;\n        font-family: "Georgia", serif;\n        color: black;\n        font-weight: bold;\n        text-align: center;\n    }\n\n    ol.zbiorowa {\n        margin: 0;\n        font-family: "Georgia", serif;\n    }\n\n    br {\n        line-height: 50%;\n    }\n</style>\n\n\n\n<p class=\'n1\'>Poniedziałek 30.08.2021r.</p>\n<p>\n\t<b>6<sup>30</sup></b> za zmarłych rodziców chrzestnych: za + Janinę Sokołowską i za + Jana Cichonia<br/>\n\t<b>6<sup>30</sup></b> za

Please help!

1 Answer 1

1

use parameters

mycursor.execute("UPDATE `table_name` SET `col_name` = &s WHERE `table_name`.`id` = 1",(doc_html,))

Soy you can avoid to handle the characters that could cause that problem

Sign up to request clarification or add additional context in comments.

3 Comments

Now I am gettin such message: mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
Ok. As i am using mysql.connector I had to insert %s instead of ?. Thanks a lot. It was a good lesson.
than you have a different query, that would ahppen when you have instead of ,(doc_html,)` added more variables without adding ? to the query for example for id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.