python 3.x - sqlalchemy insert - string argument without an encoding -
the code below worked when using python 2.7, raises statementerror when using python 3.5. haven't found explanation online yet.
why doesn't sqlalchemy accept simple python 3 string objects in situation? there better way insert rows table?
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 table created function:
def create_table(): engine = create_engine('mysql+pymysql://root:password@localhost/database_name') metadata = metadata() # create table 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)
edit full error:
>>> 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 key in compiled_params file "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 610, in <genexpr> 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 encoding above exception direct cause of 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 key in compiled_params file "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 610, in <genexpr> 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 encoding [sql: 'insert table_name (updated, site_name, site_id, p_profile) values (%(updated)s, %(site_name)s, %(site_id)s, %(p_profile)s)']
as univerio mentioned, solution encode string follows:
conn.execute(table_name.insert().values(updated=true, site_name=site_name, site_id=site_id, p_profile=bytes(json.dumps(p_profile), 'utf8')))
blobs require binary data, need bytes
in python 3 , str
in python 2, since python 2 strings sequences of bytes.
if want use python 3 str
, need use text instead of blob.
Comments
Post a Comment