mysql select 报错

代码片段:

     sql_url = "select * from webpage where url = '%s'" % b
     
     try:
         cursor.execute(sql_url)
         results = cursor.fetchall()
     except Exception, e:
         print e
         pass


系统:centos

语言:python

报错信息:

(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 '''' at line 1")

 

报错信息说 在‘“'附近有语法错误,

但是大部分数据都很顺利的跑起来,然后我把错误时候的那条数据给打印出来,发现那个url后面有 xxx='xxxxxxxx'

所以把这个url塞进sql语句,就成了 select * from webpage where url ='http://www.baidu.com/index.html?xxx='xxxxxxxxx''

哈哈,看出来了吧?

那个参数的后单引号和url的后单引号合并在一起了,本来是2个的,结果成1个了。

 

正确的写法是:

sql_url = 'select * from webpage where url = "%s"' % b

 

posted @ 2015-01-08 20:09  开开甲  阅读(402)  评论(0编辑  收藏  举报