1
votes

I am trying to INSERT some values into my database

urls =("http://www.**********.net/?page=match")
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(urls,headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)

tournament = soup.find('div',{'class':['content-body']})
match_times = tournament.find_all("div", style = "width:10%; float:left;")
match_names = tournament.find_all("div", style = "width:46%; float:left; margin-left:2%; margin-right:2%")
tournys = tournament.find_all("div", style = "width:40%; float:left; overflow:hidden;")


for element in zip(match_times, match_names, tournys):
    results = element[0].text, element[1].text, element[2].text
    matchday=datetime.strptime(results[0], "%d/%m/%y").strftime("%d-%m-%Y")
    info= results[1],results[2],matchday
    conn= MySQLdb.connect(host='localhost',user='root',passwd='',db='sport')
    c = conn.cursor()
    query = "INSERT INTO todaysmatches (match, tournament, matchdate) VALUES (%s,%s,%s)"
    c.executemany(query, info)
    conn.commit()
    conn.close()
    print  info
    print '==================='

match is a varchar(150) , tournament varchar(150) and matchdate is DATE

this is an example of a printed info:

(u'\n\xa0bestest \n9 - 16\ntryPANTS\xa0\n\n', u'\xa0Razer CS:GO Tournament', '08-12-2013')
1
try info= results[1].strip(), results[2].strip(), matchday and try what @avoid3d mentioned - Syed Habib M
changed the error to VALUES \n('Rasta.eon\\n2 - 1\\nRasta.Xd','Razer CS:GO' at line 1") - user3386406
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'match,tournamentname,matchdate) VALUES \n(' Rasta.eon 2 - 1 Rasta.Xd ','Razer C' at line 1") - user3386406
the same error this is one of the lists when I print info [u' Rasta.eon 2 - 1 Rasta.Xd ', u'Razer CS:GO Tournament 2', u'26-02-2014'] - user3386406
Can you try info= str(results[1].strip()), str(results[2].strip()), matchday and c.execute(query, info). I think it should work. - Syed Habib M

1 Answers

0
votes

Try changing the line:

# This expects a list as it's second argument, you are giving it a tuple.
c.executemany(query, info)

To this:

c.executemany(query, [info])

If that works, then you may want to read the docs, and find out if there is a better way of inserting data when you only have to insert one row. (There almost certainly is.)