问答详情
源自:2-8 保存数据

?怎么插入到sql server数据库中,mssql?

?怎么插入到sql server数据库中,mssql?

提问者:weixin_慕粉7099041 2019-09-11 14:20

个回答

  • qq_慕仙4218167
    2019-12-23 21:39:25

    这是我的代码  插入到mysql中
    # -*- coding: utf-8 -*-
    import pymysql
    # Define your item pipelines here
    #
    # Don't forget to add your pipeline to the ITEM_PIPELINES setting
    # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
    
    
    class DoubanPipeline(object):
        def __init__(self):
            self.mydb = pymysql.connect(host="127.0.0.1",user="root",password="020202",db="douban",charset="utf8",use_unicode=True)
            self.cursor = self.mydb.cursor()
    
        def process_item(self, item, spider):
            insert_sql = """insert into movie(serial_number, movie_name, introduce, star, evaluate, describetion)
                                    values (%s, %s, %s, %s,%s,%s)"""
            # 使用`占位符
            self.cursor.execute(insert_sql,(item['serial_number'],item['movie_name'],item['introduce'],item['star'],item['evaluate'],item['describetion']))
            #用commit()才会提交到数据库
            self.mydb.commit()
        def close_spider(self,spider):
            self.cursor.close()
            self.mydb.close()