Menu

fetchall() returns quotes,parens with values

Help
2004-10-07
2012-09-19
  • Juris Program

    Juris Program - 2004-10-07

    Basically when I use my python function (shown below) to get 1 column from the database, I get the values but they look like this:
    (('hamster',), (None,), ('',), ('',), ('',), ('Hamster',), ('none',), ('Human',)
    )
    I don't understand where all of the extra quotes and parentheses are coming from. A similar function returns a row and the same values aren't mangled like above, so I must assume those "'s and )'s don't exist in the actual data.

    Here is my python function. Am I doing something wrong?

    def getFrequentColumnVals(self,columnName,tableName):
    stmt='SELECT ' + columnName + ' FROM ' + tableName
    self._cursor.execute(stmt)
    #TempList=[str(item).upper().strip() for item in self._cursor.fetchall() if len(str(item).strip())>1]
    temp=self._cursor.fetchall()
    print temp
    TempList=[str(item).upper().strip() for item in temp if len(str(item).strip())>1]
    MostPop=[item[0] for item in gregutils.freq(TempList)][:10]
    return MostPop

    Sorry if tabs get garbulated in the posting. Note: the output pasted above comes from the print temp line in above function. Thanks in advance.

    Greg

     
    • Greg Fortune

      Greg Fortune - 2004-10-07

      Add a
      print stmt
      after
      stmt = ......

      I think you'll find that columnName is actually multiple column names. If that's not the case, I'd guess your INSERT statements are storing values incorrectly.

       
      • Juris Program

        Juris Program - 2004-10-07

        Here's the stmt printed:

        SELECT species FROM pet

        And here's the output from fetchall():

        (('hamster',), (None,), ('',), ('',), ('',), ('Hamster',), ('none',), ('Human',)
        )

        I'm fairly sure I can also rule out the idea of storing values incorrectly because I have other functions reading these same values and looking normal. However to further investigate, I fired up mysql.exe:

        mysql> use practice
        Database changed
        mysql> SELECT species FROM pet
        -> ;
        +---------+
        | species |
        +---------+
        | hamster |
        | NULL |
        | |
        | |
        | |
        | Hamster |
        | none |
        | Human |
        +---------+
        8 rows in set (0.02 sec)

        mysql>

        So everything looks ok here, right? This is a strange problem, huh?

        Greg

         
        • Andy Dustman

          Andy Dustman - 2004-10-07

          OK, so what's your issue? The results from fetchall() match your mysql.exe results exactly. Think about it.

           
          • Andy Dustman

            Andy Dustman - 2004-10-07

            I'll spell it out for you.

            fetchall() returns a sequences of rows.

            A row is a sequence of columns.

            Row sequences are constructed from tuples.

            A tuple of one item still needs at least one comma so the Python parser can distinquish it from arithmetic grouping.

            Some of your columns have empty strings, one has a NULL.

            NULL in Python is represented by None.

            Here's your output:

            (('hamster',), (None,), ('',), ('',), ('',), ('Hamster',), ('none',), ('Human',))

            Here's your output with formatting applied:

            (
            ('hamster',),
            (None,),
            ('',),
            ('',),
            ('',),
            ('Hamster',),
            ('none',),
            ('Human',)
            )

            Try this:

            species = [ row[0] for row in cursor.fetchall() if row[0] ]

            If your Python code looks complicated, you're probably doing something wrong.

             
            • Juris Program

              Juris Program - 2004-10-08

              Thanks Mr. Dustman. It all makes sense now.

               
    • Juris Program

      Juris Program - 2004-10-07

      Ok, I think I see what you're saying. I requested a table but with 1 column. So the result should contain a nested list where each sublist is a row?

      That would make sense. All that's left then is that stray comma as in ('Hamster',)

      Am I on the right track there, Andy?

      Greg

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.