【问题标题】:KeyError while using Google Cloud SQL使用 Google Cloud SQL 时出现 KeyError
【发布时间】:2012-02-01 12:41:13
【问题描述】:

我无法弄清楚我的代码有什么问题。 我是否以错误的方式使用了 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'>

【问题讨论】:

    标签: python google-app-engine google-cloud-sql


    【解决方案1】:

    来自谷歌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)]
    

    意味着您的参数类型之一没有编码器。如果您的任何参数不是Any tuple, string, numeric, or datetime object,那么这将是您的罪魁祸首。如果不明显哪个是错误参数,则在调用之前打印/记录每个参数的类型以确定哪个是错误的。

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2018-07-23
      • 2021-02-18
      • 2013-05-05
      • 1970-01-01
      相关资源
      最近更新 更多