2012-03-26 47 views
1

我在執行SQL語句,並且遇到了一些編碼問題。轉義和編碼後,它返回一個我無法識別的語法錯誤。使用mysqldb在Python中執行大型SQL語句時發生Econding/char轉義

這個說法由下面的代碼構成:

dbquery = "INSERT INTO series (id,nombreSerie,imdbUrl,urlPoster,descripcionEs,descripcionEn,rating,episodiosEn,episodiosEs,estado,firstAired,airDay,Network) VALUES (" + t['gossip girl']['id']+ "," + t['gossip girl']['seriesname'] + ',http://imdb.com/title/' + t['gossip girl']['imdb_id'] + "," + t['gossip girl']['poster'] + "," + t['gossip girl']['overview'][:400] + "... ,"+ t['gossip girl']['overview'][:400] + "... ," + t['gossip girl']['rating'] + "," + total + "," + total + "," + t['gossip girl']['status'] + "," + t['gossip girl']['firstaired'] + "," + t['gossip girl']['airs_dayofweek'] + "," + t['gossip girl']['network']+")" 
cursor.execute (MySQLdb.escape_string(dbquery)) 

打印執行返回如下:

print MySQLdb.escape_string(dbquery) 
INSERT INTO series (id,nombreSerie,imdbUrl,urlPoster,descripcionEs,descripcionEn,rating,episodiosEn,episodiosEs,estado,firstAired,airDay,Network) VALUES (80547,Gossip Girl,http://imdb.com/title/tt0397442,http://www.thetvdb.com/banners/posters/80547-6.jpg,Gossip Girl satirizes and revolves around the lives of young Manhattan socialites growing up on New York City\'s Upper East Side who attend elite academic institutions while dealing with sex, drugs, jealousy, and other teenage issues.... ,Gossip Girl satirizes and revolves around the lives of young Manhattan socialites growing up on New York City\'s Upper East Side who attend eliteacademic institutions while dealing with sex, drugs, jealousy, and other teenage issues.... ,8.1,0x11|1x18|2x25|3x22|4x22|5x24|,0x11|1x18|2x25|3x22|4x22|5x24|,Continuing,2007-09-19,Monday,The CW) 

而我得到的錯誤是這個:

File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in default errorhandler raise errorclass, errorvalue 
_mysql_exceptions.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 'Girl,http://imdb.com/title/tt0397442,http://www.thetvdb.com/banners/posters/8054' at line 1") 

我知道大字符串上的逗號可能會影響sql語法,但我已經替換了逗號d它似乎不影響查詢執行。

很抱歉,如果這是很簡單的,但我還沒有在網上找到了(也許我不是尋找好)

預先感謝您

回答

0

字符串必須有'周圍的任何答案,你應該有

Values (80547,'Gossip Girl','http://imdb.com/title/tt0397442,http://www.thetvdb.com/banners/posters/80547-6.jpg', etc 
+0

謝謝Niek de Klein!我正在搞這個,但我不太習慣寫SQL語句。 – 2012-03-26 16:09:45