0

TypeError: 'str' object is not callableINSERT INTO database.

我正在使用:Python 2.7.5、Scrapy 0.18 和数据库 MySQL

我的追溯是:

Traceback (most recent call last):
      File "C:\python27\lib\threading.py", line 781, in __bootstrap
        self.__bootstrap_inner()
      File "C:\python27\lib\threading.py", line 808, in __bootstrap_inner
        self.run()
      File "C:\python27\lib\threading.py", line 761, in run self.__target(*self.__args, **self.__kwargs)
    --- <exception caught here> ---
      File "C:\python27\lib\site-packages\twisted\python\threadpool.py", line 191, in _worker
        result = context.call(ctx, function, *args, **kwargs)
      File "C:\python27\lib\site-packages\twisted\python\context.py", line 118, in callWithContext
        return self.currentContext().callWithContext(ctx, func, *args, **kw)
      File "C:\python27\lib\site-packages\twisted\python\context.py", line 81, in callWithContext
        return func(*args,**kw)
      File "C:\python27\lib\site-packages\twisted\enterprise\adbapi.py", line 448, in _runInteraction
        result = interaction(trans, *args, **kw)

      File "apple\pipelines.py", line 49, in _conditional_insert

        'INSERT INTO job (id, company, day, hour, job, car1, car2) values (%s, %s, %s, %s, %s, %s)' ("company, day, hour, job, car1, car2 ")

exceptions.TypeError: 'str' object is not callable

这是我的代码脚本:

def _conditional_insert(self, tx, item):
    # create record if doesn't exist. 
    # all this block run on it's own thread
    tx.execute('select * from job where hour = %s', ("item['hour'], "))
    result = tx.fetchone()
    if result:
        log.msg("Item already stored in db: %s" % item, level=log.DEBUG)
    else:
        tx.execute(\
            'INSERT INTO job (id, company, day, hour, job, car1, car2) values (%s, %s, %s, %s, %s, %s)' ("company, day, hour, job, car1, car2 ")
        )
        log.msg("Item stored in db: %s" % item, level=log.DEBUG)

def handle_error(self, e):
    log.err(e)

知道我能做些什么来解决这个错误吗?

4

2 回答 2

1

%在以下行中缺少 a 。另外,我假设价值来自item论点。

'INSERT INTO job (id, company, day, hour, job, car1, car2) values (%s, %s, %s, %s, %s, %s)' ("company, day, hour, job, car1, car2 ")

应该

'INSERT INTO job (id, company, day, hour, job, car1, car2) values (%s, %s, %s, %s, %s, %s)' % (item['company'], item['day'], item['hour'], item['job'], item['car1'], item['car2'])

您还可以使用format字符串中的方法:

'INSERT INTO job (id, company, day, hour, job, car1, car2) values ({company}, {day}, {hour}, {job}, {car1}, {car2})'.format(**item)

顺便说一句,以下行可能不会达到您的预期:

tx.execute('select * from job where hour = %s', ("item['hour'], "))

它应该读

tx.execute('select * from job where hour = %s' % item['hour'])
于 2013-09-12T17:39:57.637 回答
0

我认为 OP 的意图是使用占位符和参数作为元组。

代码应如下所示:

def _conditional_insert(self, tx, item):
    # create record if doesn't exist. 
    # all this block run on it's own thread
    tx.execute('select * from job where hour = %s', (item["hour"], ))
    result = tx.fetchone()
    if result:
        log.msg("Item already stored in db: %s" % item, level=log.DEBUG)
    else:
        tx.execute(
            'INSERT INTO job (company, day, hour, job, car1, car2) values (%s, %s, %s, %s, %s, %s)',
            (item["company"], item["day"], item["hour"], item["job"], item["car1"], item["car2"])
        )
        log.msg("Item stored in db: %s" % item, level=log.DEBUG)

def handle_error(self, e):
    log.err(e)

我删除了id'INSERT INTO job (id, company,...') 中的列

于 2013-09-12T18:30:24.650 回答