mysql 允许表情符号的编码
mysql encoding to allow emoji
在我的pipeline.py
self.conn = pymysql.connect(host='localhost', user='usr154', password=PASSWORD, db='db_v1',
charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
self.cursor = self.conn.cursor()
# Table exists with all column types to "text"
my_id="a13cg"
title='hello world'
my_string = 'Top 10 Ist es ein Cityhotel? Ist es ein Schloss? Ist es ein Inselresort? '
self.cursor.execute("""insert into results values (%s,%s,%s)""",(my_id,title,my_string))
Traceback (most recent call last):
File "/home/timmy/.local/lib/python3.8/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/home/timmy/.local/lib/python3.8/site-packages/scrapy/utils/defer.py", line 154, in f
return deferred_from_coro(coro_f(*coro_args, **coro_kwargs))
File "/home/timmy/t_roman/t_roman/pipelines.py", line 44, in process_item
self.cursor.execute("""insert into results values (%s,%s,%s)""",
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1366, "Incorrect string value: '\xF0\x9F\x94\x8D' for column 'htmlSnippet' at row 1")
我尝试对值进行编码,但效果不佳
my_string = my_string.encode()
你好,我有以下填充 MySQL 的管道,它(最终)在烧瓶应用程序中使用,我如何正确存储这些值以便稍后显示它们。
问题是当 my_string 不是字符串时,您使用字节对字符串进行编码。表情符号可以通过将它们作为 unicode 发送来用作字符串。每个表情符号都有一个对应的 unicode 字符串分配给它。例如:
# grinning face
print("\U0001f600")
# grinning squinting face
print("\U0001F606")
# rolling on the floor laughing
print("\U0001F923")
您不需要转换为字节(我假设这是 scrapy MySQL 管道),因为 table 已经创建,您可以像这样使用它。
self.cursor.execute("""ALTER TABLE results CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin""")
您还可以将列类型从 text
更改为 NVARCHAR(255)
在我的pipeline.py
self.conn = pymysql.connect(host='localhost', user='usr154', password=PASSWORD, db='db_v1',
charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
self.cursor = self.conn.cursor()
# Table exists with all column types to "text"
my_id="a13cg"
title='hello world'
my_string = 'Top 10 Ist es ein Cityhotel? Ist es ein Schloss? Ist es ein Inselresort? '
self.cursor.execute("""insert into results values (%s,%s,%s)""",(my_id,title,my_string))
Traceback (most recent call last):
File "/home/timmy/.local/lib/python3.8/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/home/timmy/.local/lib/python3.8/site-packages/scrapy/utils/defer.py", line 154, in f
return deferred_from_coro(coro_f(*coro_args, **coro_kwargs))
File "/home/timmy/t_roman/t_roman/pipelines.py", line 44, in process_item
self.cursor.execute("""insert into results values (%s,%s,%s)""",
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/home/timmy/.local/lib/python3.8/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1366, "Incorrect string value: '\xF0\x9F\x94\x8D' for column 'htmlSnippet' at row 1")
我尝试对值进行编码,但效果不佳
my_string = my_string.encode()
你好,我有以下填充 MySQL 的管道,它(最终)在烧瓶应用程序中使用,我如何正确存储这些值以便稍后显示它们。
问题是当 my_string 不是字符串时,您使用字节对字符串进行编码。表情符号可以通过将它们作为 unicode 发送来用作字符串。每个表情符号都有一个对应的 unicode 字符串分配给它。例如:
# grinning face
print("\U0001f600")
# grinning squinting face
print("\U0001F606")
# rolling on the floor laughing
print("\U0001F923")
您不需要转换为字节(我假设这是 scrapy MySQL 管道),因为 table 已经创建,您可以像这样使用它。
self.cursor.execute("""ALTER TABLE results CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin""")
您还可以将列类型从 text
更改为 NVARCHAR(255)