Python 和 sqlite3 抛出错误:sqlite3.OperationalError: near "s": syntax error
我正在尝试使用 Python 和 BeautifulSoup 来抓取一些网络信息,遍历它,然后将一些片段插入到 sqlite3 数据库中.但我不断提出这个错误:
I'm trying to use Python and BeautifulSoup to scrape some web info, iterate through it and then insert some pieces into a sqlite3 DB. But I keep coming up with this error:
文件/Users/Chris/Desktop/BS4/TBTfile.py",第 103 行,在 TBTscrape 中c.执行(项目)sqlite3.OperationalError:接近s":语法错误
File "/Users/Chris/Desktop/BS4/TBTfile.py", line 103, in TBTscrape c.execute(item) sqlite3.OperationalError: near "s": syntax error
同样的代码在非常相似的抓取器上运行良好,不会抛出这些错误.这是它的一部分:
This same code works fine on a very similar scraper and does not throw these errors. Here's the portion of it:
listicle.append("INSERT INTO headlines (heds, url, image_loc, time, source) VALUES ('" + TBTheadline + "', '" + TBTurl + "', '" + imageName + "', '" + TBTpostDate + "', '" + source + "')")
else:
print "TBT item already in database"
print listicle
for item in listicle:
c.execute(item)
conn.commit()
row = c.fetchall()
print "This has been inserted succcessfully: ", item
您正在将收集的数据连接到您的 SQL 语句中.永远不要那样做,它是所有反模式之母.除了您看到的问题(可能是由于抓取的 HTML 中的 ' 或类似字符造成的),您的代码中还有一个巨大的安全漏洞(对您的情况可能重要,也可能无关紧要).
You are concatenating the collected data into your SQL statements. Never do that, it is the mother of all anti-patterns. Aside from the problems you are seeing (probably due to a ' or similar character in the scraped HTML), you have a gaping security hole in your code (may or may not matter in your case).
无论如何,sqlite3
有一个很好的方式来做你想做的事:executemany
.你的情况
Anyway, sqlite3
has a nice way of doing exactly what you want: executemany
. In your case
listicle.append((TBTheadline,TBTurl,imageName,TBTpostDate,source))
conn.executemany("INSERT INTO headlines (heds, url, image_loc, time, source) VALUES (?,?,?,?,?)", listicle)