简体   繁体   English

Python和sqlite3抛出错误:sqlite3.OperationalError:near“s”:语法错误

[英]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.

相关问题 sqlite3.OperationalError:靠近“WHERE”:语法错误(Python 2,sqlite3) - sqlite3.OperationalError: near “WHERE”: syntax error (Python 2, sqlite3) Python-sqlite3 sqlite3.OperationalError:接近“%”:语法错误? - Python - sqlite3 sqlite3.OperationalError: near “%”: syntax error? sqlite3.OperationalError:靠近“s”:语法错误 - sqlite3.OperationalError: near "s": syntax error sqlite3.OperationalError:“,”附近:语法错误python - sqlite3.OperationalError: near “,”: syntax error python python sqlite3.OperationalError:“-”附近:语法错误 - python sqlite3.OperationalError: near “-”: syntax error Python2.7-SQLite3库输出错误消息“ sqlite3.OperationalError:靠近“?”:语法错误” - Python2.7 - SQLite3 library outputs error message “sqlite3.OperationalError: near ”?“: syntax error” Python:sqlite3.OperationalError:在“ &lt;”附近:语法错误(使用HTML源代码更新sqlite3字段) - Python: sqlite3.OperationalError: near “<”: syntax error (updating sqlite3 field with html source code) Python SQlite3更新函数sqlite3.OperationalError:“ WHERE”附近:语法错误 - Python SQlite3 Update function, sqlite3.OperationalError: near “WHERE”: syntax error SQLite3 Python 2.7 sqlite3.OperationalError语法错误 - SQLite3 Python 2.7 sqlite3.OperationalError syntax error sqlite3“OperationalError:near”(“:语法错误”python - sqlite3 “OperationalError: near ”(“: syntax error” python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM