ankush981
ankush981

Reputation: 5417

Why aren't cursors optional in mysqlclient?

I'm quite new to Python and Flask, and while working through the examples, couldn't help noticing cursors. Before this I programmed in PHP, where I never needed cursors. So I got to wondering: What are cursors and why are they used so much in these code examples?

But no matter where I turned, I saw no clear verdict and lots of warnings:

And to top it all, I learned that MySQL does NOT support cursors!

It looks like the only code that doesn't use cursors in the mysqlclient library is the _msql module, and the author repeatedly warns not to use it for compatibility reasons: "If you want to write applications which are portable across databases, use MySQLdb, and avoid using this module directly."

Well, I hope I have explained and supported my dilemma sufficiently well. Here are two big questions troubling me:

  1. Since MySQL doesn't support cursors, what's the whole point of building the entire thing on a Cursor class hierarchy?
  2. Why aren't cursors optional in mysqlclient?

Upvotes: 4

Views: 532

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77942

Your are confusing database-engine level cursors and Python db-api cursors. The second ones only exists at the Python code level and are not necessarily tied to database-level ones.

At the Python level, cursors are a way to encapsulate a query and it's results. This abstraction level allow to have a simple, usable and common api for different vendors. Whether the actual implementation for a given vendor relies on database-level cursors or not is a totally different problem.

To make a long story short: there are two distinct concepts here:

  1. database (server) cursors, a feature that exists in some but not all SQL engines
  2. db api (client) cursors (as defined in pep 249), which are used to execute a query and eventually fetch the results.

db api cursors are named so because they conceptually have some similarity with database cursors but are technically totally unrelated.

As to why mysqlclient works this way, it's plain simple: it implements pep 249, which is the community-defined API for Python SQL databases clients.

Upvotes: 4

Related Questions