Manipulating MySQL Sets

Victor Subervi victorsubervi at gmail.com
Sun Dec 13 00:13:18 CET 2009


On Sat, Dec 12, 2009 at 6:07 PM, Victor Subervi <victorsubervi at gmail.com>wrote:

> On Sat, Dec 12, 2009 at 5:11 PM, Carsten Haese <carsten.haese at gmail.com>wrote:
>
>> Victor Subervi wrote:
>> > Hi;
>> > What type does python consider a MySQL Set??
>>
>> Let's take a look:
>>
>> >>> import MySQLdb
>> >>> conn = MySQLdb.connect(db="carsten", user="blah", passwd="blah")
>> >>> cur = conn.cursor()
>> >>> cur.execute("""
>> ...     create table pizza (
>> ...         id integer,
>> ...         toppings set('cheese','sauce','peperoni','mushrooms')
>> ...     )
>> ... """)
>> 0L
>> >>> cur.execute("""
>> ...     insert into pizza values(1, 'cheese,sauce,peperoni')
>> ... """)
>> 1L
>> >>> cur.execute("select * from pizza")
>> 1L
>> >>> rows = cur.fetchall()
>> >>> toppings = rows[0][1]
>> >>> print toppings
>> cheese,sauce,peperoni
>> >>> print type(toppings)
>> <type 'str'>
>>
>>
>> Looks like a string to me.
>>
>
> Yep, sure does, but it isn't. Again:
>
>               if isinstance(colValue[0], (str, int, long, float, long,
> complex, unicode, list, buffer, xrange, tuple)):
>                 pass
>               else:
>                 print 'XXX'
>
> and those "strings" printed triple-X. It ain't no string. Besides, if it
> were a string, I'd be able to slice it, wouldn't I? Can't slice it either.
> V
>

PS:

Changed the code to this:

            elif col[:3] != 'pic':
              if isinstance(colValue[0], (str, int, long, float, long,
complex, unicode, list, buffer, xrange, tuple)):
                pass
              else:
                print 'XXX'
                if col == 'sizes': # One of those lovely sets
                  print colValue[0][0]


throws this lovely error:

 /var/www/html/angrynates.com/cart/display.py
   96     raise
   97   cursor.close()
   98   bottom()
   99
  100 display()
display = <function display>
 /var/www/html/angrynates.com/cart/display.py in display()
   50                 print 'XXX'
   51                 if col == 'sizes':
   52                   print colValue[0][0]
   53 #                ourValue = string.split(colValue[0][6:-3], "','")
   54 #               print ourValue
colValue = (Set(['Small', 'Extra-small', 'Medium']),)

TypeError: unindexable object
      args = ('unindexable object',)

V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091212/b210014e/attachment.html>


More information about the Python-list mailing list