22. fetch all
f o r row i n c u r s o r . f e t c h a l l ( ) :
...
. . . . . .
23. or fetch each
w h i l e True :
row = c u r s o r . f e t c h o n e ( )
i f row :
...
else :
break
...
. . . . . .
24. 事务
conn . s e t i s o l a t i o n l e v e l ( n )
...
conn . commit ( ) #o r . r o l l b a c k ( )
. . . . . .
25. 典型的 DBAPI 应用
import p s y c o p g 2
w i t h conn a s p s y c o p g 2 . c o n n e c t ( . . . ) :
c u r = conn . c u r s o r ( )
try :
c u r . e x e c u t e ( ’ s e l e c t ∗ from t where k e y=? ’ ,
f o r row i n c u r . f e t c h o n e ( ) :
dosomthing . . .
conn . commit ( )
except e :
logger . log ( e )
conn . r o l l b a c ( )
. . . . . .