I'm trying to build a SQLite database from scraped text. Each row in the database corresponds to a string taken from a list, and for every loop another column is created and populated with new string data.
conn = sqlite3.connect('data.sqlite')
cur = conn.cursor()
cur.executescript('''
DROP TABLE IF EXISTS Data;
CREATE TABLE Data(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
words text)''')
while True
url = 'www.xyz.com'
if url == "break": break
#parse - find tag of interest
html = urllib.request.urlopen(url)
p_s = BeautifulSoup(html,'html.parser')
words = str(p_s.findAll('p',{'id':'p-5'}))
words = strip_tags(words)
words = pd.DataFrame(words)
col_number = col_number + 1
col_name = ('Group', col_number)
cur.execute('''ALTER TABLE Data ADD ? TEXT''', (col_name,))
for i,j in words.iterrows():
cur.execute('''INSERT OR IGNORE INTO Data (col_name)
VALUES (?)''',(j))
conn.commit()
When I run this code I get:
sqlite3.Operational.Error : near "?": syntax error
Where am I going wrong? Thanks, and I apologize for my sloppy code, I'm new to Python!
Groupto hold thecol_namevalues. It'll be much easier to use the result.