2014-03-06 27 views
1

我想一些值插入到我的數據庫Python的MySQLdb的插入類型錯誤:不是字符串格式化

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 '===================' 

比賽中全部轉換參數是一個varchar(150),賽爲varchar(150)和matchdate是DATE

這是印刷信息的一個示例:

(u'\n\xa0bestest \n9 - 16\ntryPANTS\xa0\n\n', u'\xa0Razer CS:GO Tournament', '08-12-2013') 
+0

嘗試'info = results [1] .strip(),results [2] .strip(),matchday'並嘗試@ avoid3d提到 –

+0

將錯誤更改爲VALUES \ n(' Rasta.eon \\ n2 - 1 \\ nRasta.Xd','Razer CS:GO'in line 1「) – user3386406

+0

ProgrammingError:(1064,」您的SQL語法有錯誤;請檢查與您的MySQL相對應的手冊服務器版本爲正確的語法使用附近'match,tournamentname,matchdate)VALUES \ n('Rasta.eon 2 - 1 Rasta.Xd','Razer C'在第1行) – user3386406

回答

0

嘗試改變行:

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

要這樣:

c.executemany(query, [info]) 

如果這樣的作品,那麼你可能想閱讀的文檔,並找出是否有插入數據時,你只需要插入一行的更好的方法。 (幾乎肯定是)

+0

現在我得到這個錯誤ProgrammingError:(1064,「你的SQL語法有錯誤;請查看與您的MySQL服務器版本相對應的手冊,以便在'match,tournament,matchdate'附近使用正確的語法)VALUES \ n('\\ n \ xa0Rasta.eon \\ n2 - 1 \\ nRasta.Xd \ xa0', '\ xa0Ra'在第1行「) – user3386406

相關問題