这里称这种方法。
item_date = item['time_y_m_d']
self.sqlite_manger._select_table_by_name(item_date)
此处table_name
为中文字符,并以" utf-8
"编码。
def _select_table_by_name(self,table_name):
c = self.conn.cursor()
c.execute("select * from sqlite_master where type='%s' and name='%s'" %('table',table_name)) # here exceptions.TypeError: 'str' object is not callable
r = c.fetchone()
if r is not None:
return True
return False
运行程序时出现以下错误:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Twisted-15.0.0-py2.7-macosx-10.6-intel.egg/twisted/internet/defer.py", line 578, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/zhang/Programs/Python/news/baidu_news/baidu_news/pipelines.py", line 44, in process_item
if self.sqlite_manger._select_table_by_name(item_date):
File "/Users/zhang/Programs/Python/news/baidu_news/baidu_news/sqlite_manager.py", line 62, in _select_table_by_name
c.execute("select * from sqlite_master where name='%s'" % item_date)
exceptions.TypeError: 'str' object is not callable
这是我的创建表sql:
def _create_table_by_date(self,date):
print 'create table %s' % date
self.conn.execute(" create table '%s' (p_id integer, title text, link text primary key, source text, time_y_m_d text, time_h_m text)" % date)
self.conn.commit()
我使用SQLite3和Python 2.7.9,我在Scrapy项目中使用上面的代码。 以下是例外情况:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Scrapy-0.24.4-py2.7.egg/scrapy/middleware.py", line 62, in _process_chain
return process_chain(self.methods[methodname], obj, *args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Scrapy-0.24.4-py2.7.egg/scrapy/utils/defer.py", line 65, in process_chain
d.callback(input)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Twisted-15.0.0-py2.7-macosx-10.6-intel.egg/twisted/internet/defer.py", line 383, in callback
self._startRunCallbacks(result)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Twisted-15.0.0-py2.7-macosx-10.6-intel.egg/twisted/internet/defer.py", line 491, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Twisted-15.0.0-py2.7-macosx-10.6-intel.egg/twisted/internet/defer.py", line 578, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/zhang/Programs/Python/news/baidu_news/baidu_news/pipelines.py", line 39, in process_item
r = self.sqlite_manger._select_key_word(self.word)
File "/Users/zhang/Programs/Python/news/baidu_news/baidu_news/sqlite_manager.py", line 51, in _select_key_word
c.execute("select * from category where title='%s'" % word)
exceptions.TypeError: 'str' object is not callable
我做错了什么?