difference between cursor and connection objects

前端 未结 3 1108
面向向阳花
面向向阳花 2020-12-29 21:09

I am confused about why python needs cursor object. I know jdbc and there the database connection is quite intuitive but in python I am confused with cursor object. Also I a

3条回答
  •  我在风中等你
    2020-12-29 21:27

    As others mention, a Connection() is the network connection to the database, and it's only real use is to return cursors. PEP-249, where DBApi 2.0 is specified, does not clearly define what exactly a connection or cursor is, nor what the close() method on each must do; only that .connect() must return an instance of .Connection , that .Connection.cursor() must return an instance of .Cursor , and .Cursor.execute() should invoke the statement provided and return the resulting rows. In particular, it does not define a .Connection.execute() , although specific implementations are free to implement them as extensions.

    Depending on those extensions is probably unwise, though, since it means you won't have as portable code. DBApi makes this two-level requirement because having an execute on the connection without an intermediate object can be difficult on some databases.

提交回复
热议问题