Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between cursor and connection objects

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 am doubtful about what is the difference between cursor.close() and connection.close() function in terms of resource release.

like image 551
Aman Deep Gautam Avatar asked May 18 '12 22:05

Aman Deep Gautam


People also ask

What is a database is cursor in connection object?

In computer science, a database cursor is a mechanism that enables traversal over the records in a database. Cursors facilitate subsequent processing in conjunction with the traversal, such as retrieval, addition and removal of database records.

What is a cursor object?

It is an object that is used to make the connection for executing SQL queries. It acts as middleware between SQLite database connection and SQL query. It is created after giving connection to SQLite database.

What is connection object in python?

To create a connection between the MySQL database and Python, the connect() method of mysql. connector module is used. We pass the database details like HostName, username, and the password in the method call, and then the method returns the connection object.

What is cursor used for in python?

A cursor is an object which helps to execute the query and fetch the records from the database. The cursor plays a very important role in executing the query.

What is a cursor object?

This is the object used to interact with the database. Do not create an instance of a Cursor yourself. Call connections.Connection.cursor (). See Cursor in the specification. Execute stored procedure procname with args.

How to create cursor object in MySQL connection?

You can create Cursor object using the cursor () method of the Connection object/class. Following are the various methods provided by the Cursor class/object. This method is used to call existing procedures MySQL database. This method is used to close the current cursor object.

Can multiple cursors share the same connection to a database?

Depending on the underlying implementation it may be possible to generate several cursors sharing the same connection to a database.

What is the use of MySQL-Connector-Python cursor?

The MySQLCursor of mysql-connector-python (and similar libraries) is used to execute statements to communicate with the MySQL database. Using the methods of it you can execute SQL statements, fetch data from the result sets, call procedures. You can create Cursor object using the cursor () method of the Connection object/class.


2 Answers

The cursor paradigm is not specific to Python but are a frequent data structure in databases themselves.

Depending on the underlying implementation it may be possible to generate several cursors sharing the same connection to a database. Closing the cursor should free resources associated to the query, including any results never fetched from the DB (or fetched but not used) but would not eliminate the connection to the database itself so you would be able to get a new cursor on the same database without the need to authenticate again.

like image 160
Toote Avatar answered Sep 24 '22 18:09

Toote


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 <module>.connect() must return an instance of <module>.Connection , that <module>.Connection.cursor() must return an instance of <module>.Cursor , and <module>.Cursor.execute() should invoke the statement provided and return the resulting rows. In particular, it does not define a <module>.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.

like image 42
SingleNegationElimination Avatar answered Sep 26 '22 18:09

SingleNegationElimination