2017-04-06 86 views
1
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:誰能幫助我

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') 

i print(sql_insert) to let you see it clear

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>"; 

i add the str and i can't find where syntax is . so i need your help ,can someone to help me?

+0

可能您在單引號或雙引號中遇到了'sql_insert'問題。如果可能的話'print(sql_insert)'並添加結果。 – marmeladze

+1

我只是添加結果 – cwl

+0

@ marmeladze我現在添加它 – cwl

回答

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;"> 
+0

我只是嘗試你的方式,但我面臨新的question.pymysql.err.InternalError:(1366,「不正確的字符串值:'\\ xEF \\ xBB \\ xBF \\ x09 cwl

+0

這是一個編碼問題。嘗試刪除/替換這些日文/中文字符或爲其添加適當的編碼。 – marmeladze

+0

它不能取代我只是想找到其他方式來編碼它。謝謝 – cwl