[英]Python and sqlite3 throwing an error: sqlite3.OperationalError: near “s”: syntax error
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. 我正在尝试使用Python和BeautifulSoup来抓取一些Web信息,迭代它然后将一些部分插入到sqlite3数据库中。 But I keep coming up with this error:
但我不断提出这个错误:
File "/Users/Chris/Desktop/BS4/TBTfile.py", line 103, in TBTscrape c.execute(item) sqlite3.OperationalError: near "s": syntax error 文件“/Users/Chris/Desktop/BS4/TBTfile.py”,第103行,TBTscrape c.execute(item)sqlite3.OperationalError:near“s”:语法错误
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
You are concatenating the collected data into your SQL statements. 您将收集的数据连接到SQL语句中。 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).
除了您所看到的问题(可能是由于HTML中的'或类似字符),您的代码中存在一个巨大的安全漏洞(在您的情况下可能或可能不重要)。
Anyway, sqlite3
has a nice way of doing exactly what you want: executemany
. 无论如何,
sqlite3
有一个很好的方式来完成你想要的: executemany
。 In your case 在你的情况下
listicle.append((TBTheadline,TBTurl,imageName,TBTpostDate,source))
conn.executemany("INSERT INTO headlines (heds, url, image_loc, time, source) VALUES (?,?,?,?,?)", listicle)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.