python往数据库里面插入一段js代码,提示错误,如何解决?

问题遇到的现象和发生背景

html_code = """ document.write(''); """
用python想往数据库里面插入一段js代码,但是报错了

问题相关代码,请勿粘贴截图
html_code = """    document.write('<img style="width: 100%;margin: 0 auto;" src="../cartoon/mlxsj/第{}话/'+i+'.jpg" />'); """
运行结果及报错内容

pymysql.err.ProgrammingError: (1064, '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 'style="width: 100%;margin: 0 auto;" src="../cartoon/mlxsj/第{}话/'+i+'.jpg" />' at line 1')

我的解答思路和尝试过的方法

从报错内容看是我插入语句写错了,我尝试过加入反斜杠但是,没有成功,问下这个要怎么弄

我想要达到的结果

想把这段js代码插入数据库

这个要根据你插入是用的字符串拼接的方式还是用的绑定变量的方式来看,
如果是字符串拼接,那么给字符串变量赋值的时候,字符串中的单引号要替换成两个单引号

html_code = html_code.replace("'","''")

因为sql中,单引号引起来的表示字符串,你直接拼含单引号的字符串时,会导致字符串被截断,因此需要使用两个单引号。
如果是用的绑定变量的话,就不存在这个问题了。