I am trying to extract some information from one table and store it in another table using Sqlite and Python. Table 1 contains a list of websites in the form of (www.abc.com). I am trying to extract the (abc) part from each row and store it in Table 2 which also maintains a count for each site. If the site already exist in Table 2, then it just increment the count.
Here the code I have:
p = re.compile('^.+\.([a-zA-Z]+)\..+$')
for row in c.execute('SELECT links FROM table1'):
link = p.match(row[0])
if link.group(1):
print(link.group(1))
c.execute('SELECT EXISTS(SELECT 1 FROM table2 WHERE site_name = ?)', (link.group(1), ))
When I run the script, it will only execute once, then I get:
Traceback (most recent call last):
File "test.py", line 43, in <module>
link = p.match(row[0])
TypeError: expected string or buffer
If I comment out the c.execute line, all the site names are printed properly. I am new to Python and Sqlite, so I am not sure what the problem is.
Any help will be great, thanks in advance.
c.execute
line there? You're iterating over a cursor, and then you tell that cursor to do a different query in the middle of the one you're iterating. What did you want to happen there?^(?:.+\.)?([a-zA-Z]+)\..+$