Encoding error with sqlalchemy and postgresql

后端 未结 2 1642
暗喜
暗喜 2021-02-19 00:30

I\'m using pyramid for a web application with a postgres database, wtforms, sqlalchemy and jinja2 and I\'m having this error when the application try to get the issues types fro

相关标签:
2条回答
  • 2021-02-19 01:15

    I use mysql and set the charset like this. It works for me.

    from sqlalchemy import create_engine
    from sqlalchemy.engine.url import URL
    
    db_url = {
        'database': 'db_name',
        'drivername': 'mysql',
        'username': 'username',
        'password': 'mypassword',
        'host': '127.0.0.1',
        'query': {'charset': 'utf8'},
    }
    
    engine = create_engine(URL(**db_url), encoding="utf8")
    
    0 讨论(0)
  • 2021-02-19 01:33

    You need to configure Psycopg2's client encoding. See the SQLAlchemy documentation:

    By default, the psycopg2 driver uses the psycopg2.extensions.UNICODE extension, such that the DBAPI receives and returns all strings as Python Unicode objects directly - SQLAlchemy passes these values through without change. Psycopg2 here will encode/decode string values based on the current “client encoding” setting; by default this is the value in the postgresql.conf file, which often defaults to SQL_ASCII. Typically, this can be changed to utf-8, as a more useful default:

    #client_encoding = sql_ascii # actually, defaults to database
                                 # encoding
    client_encoding = utf8
    

    A second way to affect the client encoding is to set it within Psycopg2 locally. SQLAlchemy will call psycopg2’s set_client_encoding() method (see: http://initd.org/psycopg/docs/connection.html#connection.set_client_encoding) on all new connections based on the value passed to create_engine() using the client_encoding parameter:

    engine = create_engine("postgresql://user:pass@host/dbname", client_encoding='utf8')
    

    This overrides the encoding specified in the Postgresql client configuration.

    The client_encoding parameter can be specified as a query string in the engine URL:

     postgresql://user:pass@host/dbname?client_encoding=utf8
    
    0 讨论(0)
提交回复
热议问题