LoginSignup
0
0

More than 5 years have passed since last update.

sqlalchemy で euc-jp を取り扱う

Last updated at Posted at 2019-02-26
error
sqlalchemy.exc.ProgrammingError: (ProgrammingError) You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings. u'INSERT INTO category (parent_id, category_name) VALUES (?, ?)' (999, '\xa4\xa2')
対策
from sqlalchemy.interfaces import PoolListener

class SetTextFactory(PoolListener):
    def connect(self, dbapi_con, con_record):
        dbapi_con.text_factory = lambda x: unicode(unescape(x), "euc-jp", "ignore")

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

def create_session():
    engine = create_engine('sqlite:///./test.db', echo=False,  listeners=[SetTextFactory()])
    Session = scoped_session(sessionmaker(autocommit=False,
                                          autoflush=False,
                                          bind=engine))

参考

sanko-:http://saygox.blogspot.jp/2013/08/sqlalchemy.html

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