pymysql.err.ProgrammingError:任何人都可以帮助我

时间:2017-04-06 13:00:24

标签: mysql python-3.x

import pymysql


f = open('C:\\Users\Allen\Desktop\html style.txt', 'rb')
array = []
for i in f.readlines():
    array.append(i.decode('utf8'))
str1 = ''.join(str(x) for x in array)

conn = pymysql.connect(user='root', host='localhost', password='cwl19940125', database='news', charset='utf8')
mycursor = conn.cursor()
sql_insert = 'INSERT INTO test(content) VALUES ("%s");' % str1
mycursor.execute(sql_insert)
conn.commit()
mycursor.close()
conn.close()

错误

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 \'"\ufeff\t<section class="_135editor" data-tools="135编辑器" data-id="85433" style\' at line 1')
  

我打印(sql_insert)让你看得清楚

    INSERT INTO test(content) VALUES "<section class="_135editor" data-tools="135编辑器" data-id="85433" style="position: relative;">

    <section class="135brush" data-style="color: inherit; box-sizing: border-box;line-height: 2em; text-align: center;" style="color: inherit; border-color: rgb(216, 40, 33); box-sizing: border-box; padding: 0px; margin: 0px;">


          </section>
</section>";
  

我添加str,我找不到语法的位置。所以我需要你的帮助,有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

更改

sql_insert = 'INSERT INTO test(content) VALUES ("%s");' % str1

sql_insert = 'INSERT INTO test(content) VALUES ('%s');' % str1
                                               ^^  ^^

请注意这里的单引号。

>>> qry = """insert into posts(title, body) values('lorem ipsum', '%s')""" %sql
>>> print(qry)
insert into posts(title, body) values('lorem ipsum', '<section class="_135editor" data-tools="135" data-id="85433" style="position: relative;">
    <section class="135brush" data-style="color: inherit; box-sizing: border-box;line-height: 2em; text-align: center;" style="color: inherit; border-color: rgb(216, 40, 33); box-sizing: border-box; padding: 0px; margin: 0px;">

答案 1 :(得分:0)

我不建议使用字符串插值。相反,

sql_insert = 'INSERT INTO test(content) VALUES (%s);'
mycursor.execute(sql_insert, str1)