반응형
create table test(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
title text
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
SyntaxError: Non-UTF-8 code starting with '\xed' in file 이라는 에러가 났다
🎤 이 특수문자를 넣는것에서 에러가 났는데
mysql에서 기본 utf-8형태는 3바이트 자료형이기 때문에 저러한 특수문자는 지원하지 않는다. 따라서 4바이트 자료형으로 바꾸어 줘야한다.
ALTER DATABASE [DB명] CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE [테이블명] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
명령어를 통해서 먼저 mysql 데이터베이스 테이블의 스트링형태의 자료형을 utf8mb4로 바꿔준다.
그러고 mysql connector 부분에서도 charset를 맞춰주면 된다.
여기 예시를 들어보면
create table test(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
title text
)
ALTER TABLE test CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
선언과 동시에 테이블 charset를 설정 할 수도 있음!
create table test(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
title text
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
위 방식으로 test 테이블을 생성하고
import pymysql
my_db = pymysql.connect(
user='root',
passwd='root',
host='127.0.0.1',
db='[데이터 베이스 명]',
charset='utf8mb4'
)
cursor = my_db.cursor(pymysql.cursors.DictCursor)
data = [{'title':'🎤'}, {'title':'🎦'}, {'title':'🎲'}]
sql = 'insert into test (title) values (%(title)s)'
이런식으로 하시면 특수 문자 이모티콘도 정상적으로 들어가는걸 볼 수 있다.
커넥터쪽에 charset을 안바꾸고 계속 에러가 나서 삽질했다 ㅜ
반응형
'Python' 카테고리의 다른 글
Python smtpllib를 이용한 이메일 보내기 (0) | 2022.03.10 |
---|---|
파이썬 regex 정규표현식으로 match된 부분 while문으로 하나씩 변경하기 (0) | 2021.05.25 |
파이썬 Turtle을 사용해서 다양한 도형 그려보기 (1) | 2021.04.21 |
numpy 정리 (0) | 2021.03.12 |
주피터 노트북(jupyter notebook) pdf 변환 (0) | 2021.03.04 |