Python 和 sqlite3 抛出错误:sqlite3.OperationalError: near "s": syntax error [英] Python and sqlite3 throwing an error: sqlite3.OperationalError: near "s": syntax error

查看:21
本文介绍了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)

这篇关于Python 和 sqlite3 抛出错误:sqlite3.OperationalError: near "s": syntax error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆