Sesame HTTP:, sesame http

Source: Internet
Author: User

Sesame HTTP:, sesame http

As long as your Scrapy Field name is the same as the database Field name. Congratulations, you can copy this SQL concatenation script. To store MySQL Data.

The specific Splicing code is as follows:

Def process_item (self, item, spider): if isinstance (item, WhoscoredNewItem): table_name = item. pop ('table _ name') col_str = ''row_str = ''for key in item. keys (): col_str = col_str + "" + key + "," row_str = "{}'{}',". format (row_str, item [key] if "'" not in item [key] else item [key]. replace ("'", "\'") SQL = "insert INTO {} ({}) VALUES ({}) ON DUPLICATE KEY UPDATE ". format (table_name, col_str [1:-1], row_str [:-1]) for (key, value) in six. iteritems (item): SQL + = "{}= '{}',". format (key, value if "'" not in value else value. replace ("'", "\'") SQL = SQL [:-2] self.cursor.exe cute (SQL) # Execute SQL self. cnx. commit () # write operation

This SQL concatenation is implemented. If the database has the same data, it will be updated. If the database does not exist, it will insert the SQL statement.

The specific implementation is the first for loop, where the key is obtained as the MySQL field name and VALUES is used as the SQL values (spliced into an inserted SQL statement)

The second for loop implements the concatenation of field name = VALUES.

And the SQL statement in the first for loop forms the insert into XXXXX on duplicate key update. If yes, update the SQL statement inserted if no data exists.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.