首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Mysqldb在Python中对大型SQL语句进行/char转义

使用Mysqldb在Python中对大型SQL语句进行/char转义
EN

Stack Overflow用户
提问于 2012-03-26 23:32:55
回答 1查看 155关注 0票数 1

我试图执行一条SQL语句,但遇到了一些编码问题。对其进行转义和编码后,它会返回一个我无法识别的语法错误。

该语句由以下代码构成:

代码语言:javascript
复制
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))

打印执行将返回以下内容:

代码语言:javascript
复制
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)

我得到的错误是这样的:

代码语言:javascript
复制
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语法,但我已经替换了逗号,它似乎不会影响查询的执行。

抱歉,如果这是非常简单的,但我还没有在网上找到任何答案(可能我没有搜索好)

提前谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-26 23:37:31

字符串必须包含'‘,您应该有

代码语言:javascript
复制
Values (80547,'Gossip Girl','http://imdb.com/title/tt0397442,http://www.thetvdb.com/banners/posters/80547-6.jpg', etc
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9875062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档