python sqlite3字符串对象不可调用

时间:2019-11-11 21:51:17

标签: python sql

import sqlite3

class DiseasesPipeline(object):

    def __init__(self):
        self.create_con()
        self.create_db()

    def create_con(self):
        self.con = sqlite3.connect('Diseases')
        self.cur = self.con.cursor()


    def create_db(self):
        self.cur.execute("""DROP TABLE IF EXISTS Diseases""")
        self.cur.execute("""
            create table diseases(
            Overview text,
            Symptoms text,
            ['When to see a doctor'] text,
            Causes text,
            ['Risk Factors'] text,
            Complications text,
            Prevention text,
            Diagnosis text,
            Treatment text,
            ['Lifestyle and home remedies'] text,
            ['Preparing for your appointment'] text,
            ['Departments that treat this condition'] text,
            URL text
            )
            """)


    def store_item(self,item):
        self.cur.execute("""insert into Diseases values (?,?,?,?,?,?,?,?,?,?,?,?,?)"""(
            item["Overview"][0],item['Symptoms'][0],item['When to see a doctor'][0],item['Causes'][0],item['Risk Factors'][0],
            item['Complications'][0],item['Prevention'][0],item['Diagnosis'][0],item['Treatment'][0],
            item['Lifestyle and home remedies'][0],item['Preparing for your appointment'][0],item['Departments that treat this condition'][0],
            item['URL'][0]))

    def process_item(self, item, spider): #the function which handles the dictionary 
        self.store_item(item)
        return item

您好,我正在尝试使用此管道将字典写入sqlite数据库。我当时存储为CSV(工作正常),但是我移动了SQL,我使用的是上面的类,将字典传递给该类,以存储在表“ Diseases”中。

我收到此错误

Traceback (most recent call last):
  File "/home/timmy/.local/lib/python3.6/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/timmy/test/pipelines.py", line 49, in process_item
    self.store_item(item)
  File "/home/timmy/test/pipelines.py", line 46, in store_item
    item['URL']))

1 个答案:

答案 0 :(得分:0)

您缺少逗号

import sqlite3

class DiseasesPipeline(object):

    def __init__(self):
        self.create_con()
        self.create_db()

    def create_con(self):
        self.con = sqlite3.connect('Diseases')
        self.cur = self.con.cursor()


    def create_db(self):
        self.cur.execute("""DROP TABLE IF EXISTS Diseases""")
        self.cur.execute("""
            create table diseases(
            Overview text,
            Symptoms text,
            ['When to see a doctor'] text,
            Causes text,
            ['Risk Factors'] text,
            Complications text,
            Prevention text,
            Diagnosis text,
            Treatment text,
            ['Lifestyle and home remedies'] text,
            ['Preparing for your appointment'] text,
            ['Departments that treat this condition'] text,
            URL text
            )
            """)


    def store_item(self,item):
        self.cur.execute("""insert into Diseases values (?,?,?,?,?,?,?,?,?,?,?,?,?)""",(
            item["Overview"][0],item['Symptoms'][0],item['When to see a doctor'][0],item['Causes'][0],item['Risk Factors'][0],
            item['Complications'][0],item['Prevention'][0],item['Diagnosis'][0],item['Treatment'][0],
            item['Lifestyle and home remedies'][0],item['Preparing for your appointment'][0],item['Departments that treat this condition'][0],
            item['URL'][0]))

    def process_item(self, item, spider): #the function which handles the dictionary 
        self.store_item(item)
        return item