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

rおよびcは両方ともint表のフィールドであることに注意してくださいadjacency

私は非常に混乱してcurRowおりcurCol、明らかにタイプintであり、%d私に を与えることを意味しますint。文字列として混乱しているpythonは何ですか?

4

1 に答える 1

0

渡す引数の置換を.execute()行うだけではないためです。%最初にエスケープを実行し、その過程で値を文字列に変換します。

%s代わりに使用してください。

于 2013-06-23T02:46:41.717 に答える