0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UnicodeEncodeError: 'charmap' codec can't encode characters in position 51-61: character maps to <undefined>

Posted at

heorku環境でmysql操作でハマった(個人用)

ローカル環境で動作確認ができていたがheroku環境でエラーになりかなりハマってしまった。
もお忘れないと思うが一応記録。
もし同じエラーでつまづいている人のエラー解決の助けになればと思います。

変更前ソース

main.py
# heorkuDB接続
conn = MySQLdb.connect(user=DB_USERNAME, passwd=DB_PASSWORD, host=DB_HOSTNAME, db=DB_NAME)

# sql実行(一部切り取り)(エラー発生箇所)
conn.execute("SELECT name FROM user")

エラー発生

エラー内容
UnicodeEncodeError: 'charmap' codec can't encode characters in position 51-61: character maps to <undefined>
訳)UnicodeEncodeError: 'charmap'コーデックは51〜61の位置の文字をエンコードできません:文字は<undefined>にマップされます

文字コードのエラーを確認。
色々調べてトライアンドエラーを繰り返しやっとたどり着いた解がこれ。

変更後ソース(接続時:charsetを指定)

main.py
# herokuDB接続時にcharsetを指定する
conn = MySQLdb.connect(user=DB_USERNAME, passwd=DB_PASSWORD, host=DB_HOSTNAME, db=DB_NAME, charset="utf8")

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?