简体   繁体   English

Python sqlite3,从for循环插入数据

[英]Python sqlite3, insert data from for loop

I have made a web scraper and would like to insert this into a database.我制作了一个 web 抓取器,想将其插入数据库。 Until now I wrote everything into an Excel and was working fine.到目前为止,我将所有内容都写入了 Excel 并且工作正常。 But now I am not able to insert the data into a DB, it is only inserting the last record of the first loop (carname etc etc).但现在我无法将数据插入数据库,它只是插入第一个循环的最后一条记录(carname 等)。 All the data of the for loops are being completely printed in my screen but not in my DB. for 循环的所有数据都完全打印在我的屏幕上,但不在我的数据库中。

This is my code, can anyone tell me how I can all data into the DB aswell.这是我的代码,谁能告诉我如何将所有数据也放入数据库中。

for cars in car_names:
    print(cars.text)

for cars2 in car_names2:
    print(cars2.text)

for price in prices:
    print(price.text)

for price2 in prices2:
    print(price2.text)

for image in images:
    print(image.get_attribute('src'))

print(carnamelist)
print(location)
print(len(images))

insert_query = f'''INSERT INTO cardata (CarName, CarModel, ImageUrl, FullPrice, Price, Location)
        VALUES ('{cars.text}', '{cars2.text}', '{image.get_attribute('src')}', '{price.text}', '{price2.text}', '{location}');
    '''
cursor.execute(insert_query)
connection.commit()

You're iterating on all the collections to print their values, but not to insert into the database.您正在迭代所有 collections 以打印它们的值,但不插入到数据库中。 Because of Python's (lax) scoping rules the iteration variables remain available after the iteration, therefore you can perform one insert using the last value of each scope, but that's it.由于 Python 的(宽松的)范围规则,迭代变量在迭代后仍然可用,因此您可以使用每个 scope 的最后一个值执行一次插入,仅此而已。

In order to insert the records you need to actually create the records and insert them eg为了插入记录,您需要实际创建记录并插入它们,例如

for name, name2, price, price2, image in zip(car_names, car_names2, prices, prices2, images):
    cursor.execute(
        "insert into cardata (carname, carmodel, imageurl, fullprice, price, location) values (?, ?, ?, ?, ?, ?)",
        (name.text, name2.text, image.getattribute('src'), price.text, price2.text, location)
    )

Do note the use of parameter substitution : in your original code, if the values contain any sort of SQL metacharacter (a single quote, usually) your query will break.请注意参数替换的使用:在您的原始代码中,如果值包含任何类型的 SQL 元字符(通常是单引号),您的查询将中断。 This also opens you up to sql injection issues (which the sites you're scraping could in fact use as defensive measures).这也会让您面临 sql 注入问题(您正在抓取的网站实际上可以用作防御措施)。

By using placeholders, the database API can know that these are "external values" and properly handle them internally however it prefers.通过使用占位符,数据库 API 可以知道这些是“外部值”并在内部以其喜欢的方式正确处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM