Don't build you sql separate from the execute (or so I was told when I was 
doing something similar)
 
cur.execute(INSERT INTO tkindbtal (kommune, komnr, i2005, i2006, i2007 \
, i2008, i2009, i2010) VALUES (%s,  %s,        %s,          %s,   %s,     %s,\
    %s,  %s) % (cols[0], cols[1], int(cols[2]), int(cols[3]), int(cols[4]), \
int(cols[5]), int(cols[6]), int(cols[7]))

                    

 

Jason
 
 
 
..·><((((º>

        -----Original Message-----
        From: tutor-bounces+jasons=adventureaquarium....@python.org 
[mailto:tutor-bounces+jasons=adventureaquarium....@python.org] On Behalf Of 
Tommy Kaas
        Sent: Friday, January 14, 2011 11:43 AM
        To: tutor@python.org
        Subject: [Tutor] no luck with sqlinsert
        
        

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

         

         

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to