简体   繁体   English

Python Pyodbc游标与数据库游标

[英]Python pyodbc cursor vs database cursor

I use python every day with a heavy emphasis on database work. 我每天都使用python来重点研究数据库工作。

Using pyodbc my standard start is something like 使用pyodbc我的标准开始是这样的

connection_hostname = pyodbc.connect('connection_string')
cursor_hostname = connection_hostname.cursor()
command_hostname = 'select * from everything_forever;'
cursor_hostname.execute('command_hostname')

and if i need want to reuse the cursor for another query instead of creating a new cursor, I can store the result set from the first query like so: 如果我需要将游标重用于另一个查询而不是创建新游标,则可以存储第一个查询的结果集,如下所示:

results_from_query = cursor_hostname.fetchall()

And move on after that. 然后继续。

This approach has worked well for me so far. 到目前为止,这种方法对我来说效果很好。

Recently, I changed jobs, and some of my new coworkers who typically use GUIs to work with their DB's started panicking when I demonstrated the above technique. 最近,我换了工作,当我演示上述技术时,一些通常使用GUI来与数据库一起工作的新同事开始感到恐慌。 What set them off was the cursor keyword. 使它们脱颖而出的是cursor关键字。 I understand cursors are a big no-no with DBs because they indicate logic not founded in set theory, tend to push the host into low/zero levels of parallelization, and RBAR type operations, but I don't believe the ODBC cursor I'm declaring above is the same as a cursor we think of when we have our SQL Server engineering and administration hats on. 我知道游标与DB的关系是很大的,因为它们表明逻辑不是建立在集合论中的,倾向于将主机推入低/零级别的并行化和RBAR类型的操作中,但是我不相信ODBC游标m的声明与我们在使用SQL Server工程和管理功能时所想到的游标相同。

Can someone explain the difference between these ODBC cursors and SQL Server type cursors (assuming I'm correct that they are different)? 有人可以解释这些ODBC游标和SQL Server类型的游标之间的区别吗(假设我是对的,他们是不同的)?

If I am incorrect, please enlighten me and tell me how i can more efficiently interface with my DBs. 如果我不正确,请启迪并告诉我如何更有效地与数据库进行交互。

Why cant you just execute directly from a connection like 为什么不能直接从连接执行

connection_hostname.execute(command_hostname)

I feel like having ODBC cursor structures as they are has something to do with allowing multiple cursors through the same connection to reduce connection cost and such. 我感觉好像拥有ODBC游标结构,因为它们与允许多个游标通过同一连接以减少连接成本等有关。 Way off base? 离基地远吗?

Database Cursors are reviled and mistrusted by DBA's, usually for good reason. 通常,出于充分的理由,数据库游标会受到DBA的谴责和不信任。 They're often a source of performance problems, and a set-based approach is almost always better. 它们通常是性能问题的根源,而基于集合的方法几乎总是更好。

http://www.databasejournal.com/features/mssql/article.php/3896206/What-Every-DBA-Ought-to-Know-About-SQL-Server-Cursors-and-Their-Alternatives.htm for example says: 例如, http ://www.databasejournal.com/features/mssql/article.php/3896206/What-Every-DBA-Ought-to-Know-About-SQL-Server-Cursors-and-Their-Alternatives.htm :

"At my work place, cursors are banned in our SQL Server standards. In order to use a cursor, we have to prove that the performance of the cursor is better than processing the rows another way. " “在我的工作场所,游标在我们的SQL Server标准中被禁止。为了使用游标,我们必须证明游标的性能优于以另一种方式处理行。”

To over-simplify, you might explain to your nervous friends that a python cursor is actually a synonym for what other languages call a recordset or resultset, and that their GUI tools are also using cursors/recordsets (but not creating a cursor on the DB!). 为了简化起见,您可能会向紧张的朋友解释,python游标实际上是其他语言称为记录集或结果集的同义词,并且它们的GUI工具也使用了游标/记录集(但未在数据库上创建游标) !)。

difference between cursor and connection objects 光标和连接对象之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM