我无法弄清楚我的代码中出了什么问题。 我是否以错误的方式使用了executemany?
代码:
class SQLTester(DirectHandler):
def get_handler(self):
from google.appengine.api import rdbms
command = u"""INSERT IGNORE INTO `ClickLog`
(`action` ,`trace_code` ,`url` ,`secret` ,`facebook_id` ,`ip` ,`time` ,`tag` ,`from_url` ,`to_url`)
values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"""
conn = rdbms.connect(instance="tagtoosql:tagtoo", database='mysql')
cursor = conn.cursor()
#logs = mydb.iter(ClickLog.all(), 500)
logs = ClickLog.all().fetch(100)
values = []
for k in logs:
values.append((k.action,
k.trace_code,
k.url,
k.secret,
k.facebook_id,
k.ip,
mydb.to_timestamp1000(k.time),
k.tag,
k.from_url,
k.to_url))
if len(values) == 100:
cursor.executemany(command, values)
values = []
cursor.executemany(command, values)
错误消息:
Debug Stack: ExceptionType: <type 'exceptions.KeyError'>
ExceptionValue: [KeyError(<class 'google.appengine.api.datastore_types.Text'>,)]
Traceback (most recent call last):
File "/base/data/home/apps/s~tagtoo-now-news/worker.356508952957634250/libs/handlers.py", line 331, in get
self.get_handler(*args, **atts)
File "/base/data/home/apps/s~tagtoo-now-news/worker.356508952957634250/dashboard/backend.py", line 509, in get_handler
cursor.executemany(command, values)
File "/base/python_runtime/python_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 352, in executemany
self.execute(statement, args)
File "/base/python_runtime/python_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 293, in execute
bv.type, bv.value = self._EncodeVariable(arg)
File "/base/python_runtime/python_lib/versions/1/google/storage/speckle/python/api/rdbms.py", line 242, in _EncodeVariable
value = self._conn.encoders[type(arg)](arg, self._conn.encoders)
KeyError: <class 'google.appengine.api.datastore_types.Text'>
答案 0 :(得分:2)
来自Google source code:
def _EncodeVariable(self, arg):
"""Converts a variable to a type and value.
Args:
arg: Any tuple, string, numeric, or datetime object.
Returns:
A (int, str) tuple, representing a JDBC type and encoded value.
Raises:
TypeError: The argument is not a recognized type.
"""
arg_jdbc_type = self._GetJdbcTypeForArg(arg)
value = self._conn.encoders[type(arg)](arg, self._conn.encoders)
您的KeyError正在
处提出self._conn.encoders[type(arg)]
意味着您的某个参数类型没有编码器。如果你的任何args不是Any tuple, string, numeric, or datetime object
那么那将是你的罪魁祸首。如果不明显哪个是坏参数,那么在调用之前打印/记录每个参数的类型以找出哪个是坏的。