我有一个Python 3.5脚本,可以在Windows上完美运行。现在我在Linux上尝试了它,我收到了以下错误:
TypeError: The first argument to execute must be a string or unicode query.
脚本正在记录SQL查询,因此我发现查询实际上是一个字节(b'我的SQL查询')。在Windows上,查询不会转换为字节。在Linux上,确实如此。为什么?我使用参数化SQL查询,如果我使用.decode('utf-8')
,我会收到以下错误消息:
AttributeError: 'str' object has no attribute 'decode'
所以我的SQL查询是一个字符串...... 我脚本的主要部分是:
df = pd.read_sql(sql_query, sql_engine)
我使用pandas 0.17.1
谢谢你的帮助!
更新 在Linux上,SqlAlchemy日志记录的输出是:
2015-12-03 02:23:44 PM | INFO | b'SELECT 1;'
2015-12-03 02:23:44 PM | INFO | ()
2015-12-03 02:23:44 PM | INFO | ROLLBACK
在Windows上,完全相同的脚本会生成以下SqlAlchemy日志记录:
2015-12-03 02:46:25 PM | INFO | SHOW VARIABLES LIKE 'character_set%%'
2015-12-03 02:46:25 PM | INFO | ()
2015-12-03 02:46:25 PM | INFO | SELECT 1;
2015-12-03 02:46:26 PM | INFO | ()
我的sql_query
SQL查询的类型为<class 'str'>
以下是完整的追溯:
Traceback (most recent call last):
File "<my script file>.py", line <xyz>, in get_df_from_dwh
df = pd.read_sql(sql_query, sql_engine)
File "/home/user/anaconda3/lib/python3.5/site-packages/pandas/io/sql.py", line 513, in read_sql
chunksize=chunksize)
File "/home/user/anaconda3/lib/python3.5/site-packages/pandas/io/sql.py", line 1181, in read_query
result = self.execute(*args)
File "/home/user/anaconda3/lib/python3.5/site-packages/pandas/io/sql.py", line 1072, in execute
return self.connectable.execute(*args, **kwargs)
File "/home/user/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1991, in execute
return connection.execute(statement, *multiparams, **params)
File "/home/user/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 906, in execute
return self._execute_text(object, multiparams, params)
File "/home/user/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1054, in _execute_text
statement, parameters
File "/home/user/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1146, in _execute_context
context)
File "/home/user/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1344, in _handle_dbapi_exception
util.reraise(*exc_info)
File "/home/user/anaconda3/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 182, in reraise
raise value
File "/home/user/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
context)
File "/home/user/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute
cursor.execute(statement, parameters)
TypeError: The first argument to execute must be a string or unicode query.