0

在 python 中,我遇到以下代码中的类型问题:

>>> curRow = 1                                                                                                                                                                                                                                                     
>>> curCol = 2                                                                                                                                                                                                                                              
>>> type(curRow)                                                                                                                                                                                                                           
type 'int'                                                                                                                                                                                                                                             
>>> cur.execute("select COUNT(*) from adjacency where r = %d and c = %d", (curRow, curCol))                                                                                                                                                   
Traceback (most recent call last):                                                                                                                                                                                                   
  File "<stdin>", line 1, in <module>                                                                                                                                                                                                                   
  File "build/bdist.macosx-10.8-intel/egg/MySQLdb/cursors.py", line 183, in execute                                                                                                                                                                    
TypeError: %d format: a number is required, not str

注意rc都是int表中的字段adjacency

我很困惑,curRow显然curCol是类型int%d意味着给我一个int。什么是 python 令人困惑的字符串?

4

1 回答 1

0

因为.execute()不只是%替换您传递的参数。它首先通过转义运行它们,在此过程中将值转换为字符串。

改为使用%s

于 2013-06-23T02:46:41.717 回答