在FOR循环中运行IF语句时,SQLite错误和退出(在第一个结果之后)

时间:2019-06-18 08:57:25

标签: python sqlite scrapy

我制造了一只抓痒的蜘蛛,可以在工作现场中奔跑,并返回我有资格的所有工作,因此我不必每天滚动浏览它们。

我正在正确解析json:

        jsonresponse = json.loads(response.body)
        for item in jsonresponse:
            yield{
                'id':item['id'],
                'date':item['date'],
                'company':item['company'],
                'position':item['position'],
                'description':item['description'],
                'url':item['url'],
                }

然后我设置了一个函数来获取将结果保存为的任何.csv文件,创建一个新的文件作为备份,然后创建一个SQLite数据库以将结果转储到:

    def close(self, reason):
        csv_file = max(glob.iglob('*.csv'), key=os.path.getctime)

        with open(csv_file) as input, open('jobs2.csv', 'w', newline='') as output:
            writer = csv.writer(output)
            for row in csv.reader(input):
                if any(field.strip() for field in row):
                    writer.writerow(row)


        db = sqlite3.connect(':memory:')
        csv_data = csv.DictReader(open('jobs.csv'))
        cur = db.cursor()
        cur.execute('''CREATE TABLE jobs_table(date TEXT PRIMARY KEY,
                                               id TEXT,
                                               company TEXT, 
                                               position TEXT,
                                               description TEXT,
                                               url TEXT)
                    ''')


        db.commit()
        print('')
        print('DB CREATED')
        print('')

然后,我要定义一个“技能”列表,以检查.csv文件中的每一行,以查看我是否符合该职位的条件:

    # skills = {'python'}
    skills = ('python')

    for row in csv_data:
        if skills in row.get('description').lower():
            print('Job Match!')
            print('')
            print('')
            print(row)
            print('')
            print('')```

这就是我遇到的问题。它打印出一个Match,并打印出FIRST结果的OrderedDict:

2019-06-18 14:59:56 [scrapy.extensions.feedexport] INFO: Stored csv feed (309 items) in: jobs.csv```


DB CREATED


Job Match!


OrderedDict([('id', '73345'), ('date', '2019-06-11T14:16:33-07:00'), ('company', 'JBS Custom Software Solutions'), ('position', 'Full Stack Developer'), ('description', 'JBS Full-Stack Developer (Python, JavaScript, PostgreSQL)Required; 3+ years working with Python    3+ years working with JavaScript; Strong knowledge of modern JavaScript development practices; Strong computer science skills'), ('url', 'https://entrenous.com/jobs/73472')])```

然后,当我尝试运行cur.execute()将行插入到“ jobs_table”中时,会出现问题。这是我尝试过的(命令)和被踢回的内容(错误):

命令: cur.execute('INSERT INTO jobs_table(date, id, company, position, description, url) VALUES(%s, %s, %s, %s, %s, %s)', row)

错误: (sqlite3.OperationalError: near "%": syntax error)

命令: cur.execute('INSERT INTO jobs_table(date, id, company, position, description, url) VALUES(?, ?, ?, ?, ?, ?)', row)

错误: (sqlite3.ProgrammingError: Binding 1 has no name, but you supplied a dictionary (which has only names).)

命令: cur.execute("INSERT INTO jobs_table(date, id, company, position, description, url) VALUES(date, id, company, position, description, url)", row)

错误: (sqlite3.OperationalError: no such column: date)

命令: cur.execute("INSERT INTO jobs_table(date, id, company, position, description, url) VALUES(:date, :id, :company, :position, :description, :url)", row)

错误: (sqlite3.IntegrityError: UNIQUE constraint failed: jobs_table.date)

命令: cur.execute("INSERT INTO jobs_table(date, id, company, position, description, url) VALUES('date', 'id', 'company', 'position', 'description', 'url')", row)

错误: (sqlite3.IntegrityError: UNIQUE constraint failed: jobs_table.date)

我用一个简单的提交,关闭和打印功能将其包装起来,让我知道一切都很好,我可以回去睡觉了,但就目前情况而言,我不能回去睡觉。

        db.commit()
        db.close()
        print("JOBS IMPORTED!")

我早些时候发布了一个类似的问题,但是对于我想从这里发布的内容并不清楚,所以这就是我想要的: 我只想保存包含[[技能]]的[描述]的工作。 他们中的其余人对我毫无用处。

有没有人可以帮助我解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果收到此错误:(sqlite3.IntegrityError:唯一约束失败:jobs_table.date)

确保以正确的顺序插入事物... 继续前进!