sqlalchemy insert - 没有编码的字符串参数

时间:2016-08-23 23:31:31

标签: python-3.x sqlalchemy pymysql

下面的代码在使用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)

编辑此表格以前是使用此功能创建的:

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)']

2 个答案:

答案 0 :(得分:3)

正如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。

答案 1 :(得分:0)

您只需要将字符串转换为字节字符串,例如:

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

or

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