我收到TypeError: 'str' object is not callable
错误消息时
INSERT 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)
知道我能做些什么来克服这个错误吗?
答案 0 :(得分:1)
您在以下行中缺少%
。另外,我假设值来自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'])
答案 1 :(得分: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)
我删除了'INSERT INTO工作中的id
列(id,company,...'