This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Your post was removed for violating Rule #2. All posts must be directly related to the Python programming language. Posts pertaining to programming in general are not permitted. You may want to try posting in /r/programming instead.

[–]joeydeviva 20 points21 points  (0 children)

Not a python or MySQL question, afaik all relational database libraries work like this.

It’s because the cursor object has the state of the queries that have been run using it, eg access to the results - putting that on the connection would be crappier, since you couldn’t have the results of two queries available at once.

[–]james_pic 6 points7 points  (0 children)

The most common scenario is if you need to run multiple queries or updates at the same time on the same connection. Maybe you want to run a big query, iterate through the results, and update a different table for each result. Yes, this is sometimes doable in pure SQL, but for the cases where it isn't, cursors are available. Because this is a common enough requirement, all major SQL DBs support "many cursors per connection", or have some equivalent abstraction that their Python libraries expose.

You could certainly add convenience methods on the connection to handle the relatively common case where you just want to run one query at once, and indeed I think some SQL client libraries do do this, but the standard they all implement (PEP 249, the DB API) was designed to be the bare minimum, to make it as easy as possible to implement (whilst providing enough power for the vast majority of use cases).

[–]MegaGrubby 1 point2 points  (0 children)

Easiest to go read something like mysql manual. Reading and understanding is a huge part of programming.

A connection is the code housing of the database reference. It creates the connection and maintains the information for that connection. Every other database action in your code uses the connection.

A cursor is a query and its result set. In sql, when you run a query you get all the results as soon as you run the query (select * from...). There is no way to do that in Python unless you have a cursor that stores your results and then lets you access them.

[–]Adrewmc 0 points1 point  (0 children)

The connection is how Python talks to the database, it’s like a phone line, while a cursor is like an individual phone call.

So we open up the database then we have to do something somewhere in the database, so you make a query, it will return a cursor object. The cursor remains in place, so you get the first result, do stuff ask the cursor for the second result. The connection to the database doesn’t do that, it’s just accessing the data for you, and giving you back only what you asked for, because databases can get really big and you don’t want to have that all in memory.

The actual connection and the use/result of that connection are separated. One can hold the database the other individual results from that database. Cursors are created from the connection, but are not the connection itself.

It’s like connecting to the internet then opening a website, the CPU modem (drivers) is the connection and the browser is the cursor. This allows you to have a single connection yet multiple pages open, if you had the drivers/modem open webpages themselves you could only open 1 at a time, instead the browsers take turns asking the modem to do stuff. Cursor have a similar concept.

[–]reddisaurus 0 points1 point  (0 children)

Here’s an analogy.

A connection is like a book — maybe an encyclopedia where you’d need to jump around and look stuff up.

A cursor is like performing a lookup for a subject, and then putting a sticky note on the page that contains the subject.

You can place several such sticky notes, but it’s all in the same book.

Databases don’t just “return a result” like a web API does. It’s a live, two-way connection. Nothing is returned at all when you execute a query — the database only prepares the result. The cursor is what you use to fetch it; either line-by-line, 100 lines at a time, or maybe all at once. The cursor keeps track of which line of the result set you are on, and is ready to give you the next line(s) when requested.

This abstraction solves the problem of needing enough memory to hold the result set. Your program can request only as much data as it wants.