pysql数据库存入的万能语句

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mjp_erhuo/article/details/81333121
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql


class HaodPipeline(object):
    def __init__(self):
        self.conn = pymysql.connect(
            host='127.0.0.1',
            port=3306,
            user='root',
            passwd='root',
            db='daikuan',
            charset='utf8mb4'
        )
        self.cur = self.conn.cursor()

    def process_item(self, item, spider):
        if not hasattr(item, 'table_name'):
            return item
        cols, values = zip(*item.items())
        sql = "INSERT INTO `{}` ({}) VALUES ({}) ON DUPLICATE KEY UPDATE {}".format(
            item.table_name,
            ','.join(cols),
            ','.join(['%s'] * len(values)),
            ','.join(['`{}`=%s'.format(k) for k in cols])
        )
        self.cur.execute(sql, values * 2)
        self.conn.commit()
        print('-' * 50 + sql)
        return item

    def close_spider(self, spider):
        self.cur.close()
        self.conn.close()

猜你喜欢

转载自blog.csdn.net/mjp_erhuo/article/details/81333121