0

write a python program to insert data into mysql tables, when running the following code, it errors:

def public_figure_db_manage(self,type, posts, public_figure_id, public_figure_name):
...
    db_data = posts
    if type == "public_figure_posts":
        for item in db_data:
            is_exist_id_sql = "select * from fb_public_figure_posts where Post_ID = '" + item['id'] + "'"
            if cur.execute(is_exist_id_sql) == 0:
                insert_sql = "INSERT INTO fb_public_figure_posts VALUES ('{0}','{1}','{2}','{3}','{4}',{5},'{6}','{7}',{8},{9},{10})".format(item['id'],item['permalink_url'],item['created_time'],item['updated_time'],item['from']['name'],item['from']['id'],item['message'],item['link'],item['likes']['summary']['total_count'],item['comments']['summary']['total_count'],public_figure_id)
                cur.execute(insert_sql)

it errors:

Traceback (most recent call last):
  File "C:/Python/PyCharmProject/FaceBookCrawl/FBCrawl.py", line 268, in <module>
    publicfigure_download.public_figure_posts_storage(public_figure_name)
  File "C:\Python\PyCharmProject\FaceBookCrawl\publicfigure_download.py", line 103, in public_figure_posts_storage
    mysql_manage().public_figure_db_manage(type, posts, public_figure_id, public_figure_name)
  File "C:\Python\PyCharmProject\FaceBookCrawl\database_manage.py", line 42, in public_figure_db_manage
    cur.execute(insert_sql)
  File "C:\Python\Python36\lib\site-packages\pymysql\cursors.py", line 166, in execute
    result = self._query(query)
  File "C:\Python\Python36\lib\site-packages\pymysql\cursors.py", line 322, in _query
conn.query(q)
  File "C:\Python\Python36\lib\site-packages\pymysql\connections.py", line 833, in query
    sql = sql.encode(self.encoding, 'surrogateescape')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2019' in position 242: ordinal not in range(256)


Process finished with exit code 1

it point that the sentence errors:

cur.execute(insert_sql)

it seems a problem of code, could you please help me

bin
  • 71
  • 1
  • 5
  • 1
    You asked 3 questions yesterday, which were either answered or received meaningful comments; however there has been no follow-up from you --- why should we bother? – John Machin Jan 22 '17 at 23:36
  • 2
    DUPLICATE: google search on the error message returns 3 other questions on SO plus a few hundred on other sites. You can access the 3 other SO questions by clicking on them in the "Related" panel on the right-hand side of your question. – John Machin Jan 23 '17 at 00:03
  • 3
    Possible duplicate of [UnicodeEncodeError: 'latin-1' codec can't encode character](http://stackoverflow.com/questions/3942888/unicodeencodeerror-latin-1-codec-cant-encode-character) – ivan_pozdeev Jan 23 '17 at 23:19
  • Possible duplicate of [Python & MySql: Unicode and Encoding](http://stackoverflow.com/questions/8365660/python-mysql-unicode-and-encoding) –  Jan 24 '17 at 04:03

0 Answers0