Skip to content Skip to sidebar Skip to footer

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. But I keep coming up with this error: File '/U

Solution 1:

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).

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)

Post a Comment for "Python And Sqlite3 Throwing An Error: Sqlite3.operationalerror: Near "s": Syntax Error"