我试图执行SQL语句并遇到编码问题。转义并编码后,它会返回一个我无法识别的语法错误。
该陈述由以下代码组成:
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语法,但我已经替换了逗号,它似乎不会影响查询执行。
很抱歉,如果这很简单,但我没有在网上找到任何答案(也许我搜索不好)
提前谢谢
答案 0 :(得分:0)
字符串必须“围绕它们,你应该有
Values (80547,'Gossip Girl','http://imdb.com/title/tt0397442,http://www.thetvdb.com/banners/posters/80547-6.jpg', etc