If you put the data in MySQL by yourself, it will not be garbled if you take it out with sqlalchemy, but if you put it in the dump file that you got from a friend, it will be garbled if you take it out with sqlalchemy / (^ o ^) \
When using sqlalchemy to move data in and out, ** Insert and extract (select) always use the same character code (setting). ** **
This time, the following basics? I was trying to make a good connection.
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine,
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import MetaData
#Processing related to connection with db
engine = create_engine('mysql://username:password@localhost/dbname?charset=utf8')
Base = declarative_base()
metadata = MetaData()
Session = sessionmaker(bind=engine)
session = Session()
There is no fatal problem if you put it in and out by yourself. However, be careful when handling data received from other people with sqlalchemy.
This time, I got a dump file of mysql and inserted the data, but when I took it out via sqlalchemy, the characters were garbled / (^ o ^) \
myself:
create_engine('mysql://username:password@localhost/dbname')
charset is not set to utf-8 / (^ o ^) \
Opponent: The charset was properly set to utf-8. (I didn't use sqlalchemy because I was inserting data into MySQL with rails.)
I rewrote it as follows.
create_engine('mysql://username:password@localhost/dbname?charset=utf8')
I just added? Charset = utf8 at the end. However, the following flow is not good.
Insert data (without? Charset = utf8) ↓ Extract data (with? Charset = utf8 added) The characters are garbled.
Let's insert and retrieve data with the same settings. In other words
Insert data (with? Charset = utf8 added) ↓ Extract data (with? Charset = utf8 added)
This way, garbled characters will not occur.
And vice versa. If you inserted the data without? charset = utf8, retrieve it without? charset = utf8.
If you retrieve the data with? Charset = utf8 attached, the character data will be returned in Unicode. If you did not add? charset = utf8, it was returned ascii.
I want to make Python3 as soon as possible \ (^ o ^) /
Recommended Posts