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 8819687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:25:26+00:00 2026-06-14T05:25:26+00:00

I have the following code: def executeQuery(conn, query): cur = conn.cursor() cur.execute(query) return cur

  • 0

I have the following code:

def executeQuery(conn, query):
    cur = conn.cursor()
    cur.execute(query)
    return cur

def trackTagsGenerator(chunkSize, baseCondition):
    """ Returns a dict of trackId:tag limited to chunkSize. """

    sql = """
        SELECT track_id, tag
        FROM tags 
        WHERE {baseCondition}
    """.format(baseCondition=baseCondition)

    limit = chunkSize
    offset = 0
    while True:
        trackTags = {}

        # fetch the track ids with the coresponding tag
        limitPhrase = " LIMIT %d OFFSET %d" % (limit, offset)
        query = sql + limitPhrase
        offset += limit
        cur = executeQuery(smacConn, query)
        rows = cur.fetchall()

        if not rows:
            break

        for row in rows:
            trackTags[row['track_id']] = row['tag']

        yield trackTags

I want to use it like this:

for trackTags in list(trackTagsGenerator(DATA_CHUNK_SIZE, baseCondition)):        
    print trackTags
    break

This code produces the following error without even fetching one chunk of track tags:

Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in <bound method SSDictCursor.__del__ of <MySQLdb.cursors.SSDictCursor object at 0x10b067b90>> ignored

I suspect it’s because I have the query execute logic in the body of loop in the generator function.

Is someone able to tell me how to fetch chunks of data using mysqldb in such way?

  • 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-06-14T05:25:27+00:00Added an answer on June 14, 2026 at 5:25 am

    I’m pretty sure this is because it can run into situations where you’ve got two queries
    running simultaniously because of the yield. Depending on how you call the function (threads, async, etc..) I’m pretty sure your cursor might get clobbered too?

    As well, you’re opening yourself up to (sorry, but I can’t sugar coat this part) horrific SQL injection holes by inserting baseConditional using essentially a printf. Take a look at the DB-API’s parameter substitution docs for help.

    Yield isn’t going to save you time or energy here at all, the full sql command will always need to run before you’ll get a single result. (Hence you’re using LIMIT and OFFSET to make it more friendly, kudos)

    i.e. someone updates the table while you’re yielding out some data, in this particular case – not the end of the world. In many others, it gets ugly.

    If you’re just goofing around and you want this to work ‘right-now-dammit’, it’d probably work to modify executeQuery as such:

     def executeQuery(conn, query):
         cur = conn.cursor()
         cur.execute(query)
         cur = executeQuery(smacConn, query)
         rows = cur.fetchall()
         cur.close()
         return rows
    

    One thing that also kinda jumps out at me – you define trackTags = {}, but then you update tagTrackIds, and yield trackTags.. Which will always be empty dict.

    My suggestion would be to not bother yourself with the headache of hand writing SQL if you’re just trying to get a hobby project working. Take a look at Elixir which is built on top of SQLAlchemy.

    Using an ORM (object-relational-mapper) can be a much more friendly introduction to databases. Defining what your objects look like in Python, and having it automatically generate your schema for you – and being able to add/modify/delete things in a Pythonic manner is really nifty.

    If you really need to be async, check out ultramysql python module.

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

Sidebar

Related Questions

I have the following code: def query(self,query): lock = QMutexLocker(self.mutex) reply = self.conn.query(query) if
I have the following code: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)
I have a following simple code: def get(): return [lambda: i for i in
I have the following code: def causes_exception(lamb): try: lamb() return False except: return True
I have the following piece of code def show unless logged_in? login_required return end
Hi I have the following HQL query which I am running using executeQuery(): def
I have the following code: def commandType(self): import re print self.cmds[self.counter] if re.match(@,self.cmds[self.counter]): return
I have following code: def whatever(url, data=None): req = urllib2.Request(url) res = urllib2.urlopen(req, data)
I have the following code: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing
i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor

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.