[Tutor] no luck with sqlinsert
Tommy Kaas
tommy.kaas at kaasogmulvad.dk
Fri Jan 14 17:43:22 CET 2011
I get a invalid syntax error when I try to run this script - and it's
con.commit() which is highlighted when I get the error.
I can't see what is wrong. I think it looks like other scripts I'm running
without problems. The scraping part works fine. And the table exists in the
mysql db. I have just separated in an attempt to locate the problem. But no
luck. I hope someone can spot the problem.
(ActivePython 2.6.6. on pc/win)
TIA
import urllib2
import MySQLdb
from BeautifulSoup import BeautifulSoup
con = MySQLdb.connect(host='mysql2.dicar.dk', user='python1',
passwd='python1', db='python')
cur = con.cursor()
sqlinsert = '''
INSERT INTO tkindbtal (kommune, komnr, i2005, i2006, i2007, i2008, i2009,
i2010)
VALUES (%s, %s, %s, %s, %s, %s, %s,
%s)
'''
soup =
BeautifulSoup(urllib2.urlopen('http://www.kaasogmulvad.dk/unv/python/kom_ind
btal.htm').read())
rows = soup.findAll('tr')
print rows
for tr in rows[1:]:
cols = tr.findAll('td')
try:
cur.execute(sqlinsert, (cols[0], cols[1], int(cols[2]),
int(cols[3]), int(cols[4]), int(cols[5]), int(cols[6]), int(cols[7]))
con.commit()
except:
con.rollback()
con.close()
Tommy Kaas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110114/f953e0eb/attachment.html>
More information about the Tutor
mailing list