【问题标题】:sqlalchemy insert - string argument without an encodingsqlalchemy 插入 - 没有编码的字符串参数
【发布时间】:2016-12-30 22:27:02
【问题描述】:

以下代码在使用 Python 2.7 时有效,但在使用 Python 3.5 时引发了 StatementError。我还没有在网上找到一个很好的解释。

为什么 sqlalchemy 在这种情况下不接受简单的 Python 3 字符串对象?有没有更好的方法将行插入到表中?

from sqlalchemy import Table, MetaData, create_engine
import json

def add_site(site_id):
    engine = create_engine('mysql+pymysql://root:password@localhost/database_name', encoding='utf8', convert_unicode=True)
    metadata = MetaData()
    conn = engine.connect()
    table_name = Table('table_name', metadata, autoload=True, autoload_with=engine)

    site_name = 'Buffalo, NY'
    p_profile = {"0": 300, "1": 500, "2": 100}

    conn.execute(table_name.insert().values(updated=True,
                                    site_name=site_name, 
                                    site_id=site_id, 
                                    p_profile=json.dumps(p_profile)))

add_site(121)

EDIT 该表之前是使用此函数创建的:

def create_table():
    engine = create_engine('mysql+pymysql://root:password@localhost/database_name')
    metadata = MetaData()

    # Create table for updating sites.
    table_name = Table('table_name', metadata,
        Column('id', Integer, Sequence('user_id_seq'), primary_key=True),
        Column('updated', Boolean),
        Column('site_name', BLOB),
        Column('site_id', SMALLINT),
        Column('p_profile', BLOB))

    metadata.create_all(engine)

编辑完全错误:

>>> scd.add_site(121)
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1073, in _execute_context
    context = constructor(dialect, self, conn, *args)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 610, in _init_compiled
    for key in compiled_params
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 610, in <genexpr>
    for key in compiled_params
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/sqltypes.py", line 834, in process
    return DBAPIBinary(value)
  File "/usr/local/lib/python3.5/dist-packages/pymysql/__init__.py", line 79, in Binary
    return bytes(x)
TypeError: string argument without an encoding

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user1/Desktop/server_algorithm/database_tools.py", line 194, in add_site
    failed_acks=json.dumps(p_profile)))
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 914, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1078, in _execute_context
    None, None)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1341, in _handle_dbapi_exception
    exc_info
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 185, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1073, in _execute_context
    context = constructor(dialect, self, conn, *args)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 610, in _init_compiled
    for key in compiled_params
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 610, in <genexpr>
    for key in compiled_params
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/sqltypes.py", line 834, in process
    return DBAPIBinary(value)
  File "/usr/local/lib/python3.5/dist-packages/pymysql/__init__.py", line 79, in Binary
    return bytes(x)
sqlalchemy.exc.StatementError: (builtins.TypeError) string argument without an encoding [SQL: 'INSERT INTO table_name (updated, site_name, site_id, p_profile) VALUES (%(updated)s, %(site_name)s, %(site_id)s, %(p_profile)s)']

【问题讨论】:

  • 请提供您的模型声明和完整堆栈跟踪。
  • 为此,我只是使用 SQL 表达式语言,而不是对象关系映射器,所以我没有模型声明。我的理解是任何一种方法都应该有效。
  • 添加了一个编辑以显示表格的创建方式(如果有帮助)。
  • 您正在将 str 对象传递给 site_name,但它需要 bytes(因为它是 BLOB)。

标签: python-3.x sqlalchemy pymysql


【解决方案1】:

正如 univerio 所提到的,解决方案是将字符串编码如下:

conn.execute(table_name.insert().values(updated=True,
                                    site_name=site_name, 
                                    site_id=site_id, 
                                    p_profile=bytes(json.dumps(p_profile), 'utf8')))

BLOB 需要二进制数据,因此我们在 Python 3 中需要 bytes,在 Python 2 中需要 str,因为 Python 2 字符串是字节序列。

如果我们想使用 Python 3 str,我们需要使用 TEXT 而不是 BLOB。

【讨论】:

  • 或者我们可以这样做:p_profile=json.dumps(p_profile).encode('utf-8')。 SQLAlchemy 在执行插入时会自动将其转换为字节。
【解决方案2】:

您只需要将字符串转换为字节字符串即可:

                                    site_name=str.encode(site_name), 
                                    site_id=site_id, 
                                    p_profile=json.dumps(p_profile)))```

or

```site_name = b'Buffalo, NY'```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 2015-09-18
    • 2019-01-28
    • 2018-10-16
    • 2017-03-23
    • 1970-01-01
    • 2020-04-22
    相关资源
    最近更新 更多