Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:50:06+00:00 2026-05-28T04:50:06+00:00

I make multiple queries to my DB from my python code that uses pyodbc

  • 0

I make multiple queries to my DB from my python code that uses pyodbc to connect to a SQL Server.

cursor = connection.connect(connection_string)
qry1 = "Select * FROM Table1"

rows = cursor.execute(qry1).fetchall()

for row in rows:
   qry2 = "Select * FROM Table2"

   rows2 = cursor.execute(qry2).fetchall()

   for row2 in rows2:
      print row2

This works and successfully goes through both queries to completion.

How is it that I can reuse the cursor object?

I would think when I ran

rows2 = cursor.execute(qry2).fetchall()

That rows and rows2 would now point to the same thing (object).

EDIT: Useful info from pyodbc site

Since this reads all rows into memory, it should not be used if there are a lot of rows. Consider iterating over the rows instead. However, it is useful for freeing up a Cursor so you can perform a second query before processing the resulting rows.

cursor info

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:50:06+00:00Added an answer on May 28, 2026 at 4:50 am

    I think your last statement is false, as .fetchall() returns a new list containing all the rows returned by the cursors query, after which you may reuse the cursor. (but i’d test what the cost of a new cursor is before thinking about reusing them.) [well, i don’t know if this is really true but it is for the dbapi compatible sqlite3 bindings]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i make multiple, composite query in oracle? for example this several queries
I need to make multiple divs move from right to left across the screen
I'm trying to make a webpage that allows the uploading of multiple files at
I'm trying to make a search feature that will search multiple columns to find
I can use the following code for tiny little queries: DECLARE @sql VARCHAR(8000) SET
When I write SQL queries, I find myself often thinking that there's no way
How can I get build a QuerySet that gets multiple rows from django? I
I am trying to retrieve around 200 billion rows from a remote SQL Server.
I have multiple linq queries that retrieve the same data just at different grouping
I heard in Microsoft SQL Server there are multiple ways to find worst stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.