MySQLdb - Tuples

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lajos Kuljo

    #1

    MySQLdb - Tuples

    Hallo,
    ich bin voll neu im Python-Programming, deshalb ist mein Problem
    wahrscheinlich trivial:

    Wenn ich die Script
    ############### ############### ###########33
    #! /usr/bin/env python
    import MySQLdb
    db=MySQLdb.conn ect(host='local host', db='photum_0_6_ 2', user='root',
    passwd='thkhgfg d')
    c=db.cursor()
    c.execute('sele ct person from persons order by person')
    tup=c.fetchall( )
    for a in tup:
    print a
    ############### ############### ############### ###
    ausführe, erhalte ich Dinge wie
    ('Dieter',)
    ('Hulda',)
    .....

    Ich brauche die Klammern, Apostrphe und Kommas nicht (ich will nur die
    Inhalte) und kann ich sie nicht loswerden. Versucht habe ich u. a. print
    a[2:-3] was nur zwei Klammern bringt.
    b=lstrip(a, "(") haut auch nicht hin.
    Weiss jemand Rat?

    P.S. Woher kommen diese Klammern und Apostrophen, vom MySQLdb?
  • Dennis Benzinger

    #2
    Re: MySQLdb - Tuples

    Lajos Kuljo wrote:[color=blue]
    > Hallo,
    > ich bin voll neu im Python-Programming, deshalb ist mein Problem
    > wahrscheinlich trivial:
    >
    > Wenn ich die Script
    > ############### ############### ###########33
    > #! /usr/bin/env python
    > import MySQLdb
    > db=MySQLdb.conn ect(host='local host', db='photum_0_6_ 2', user='root',
    > passwd='thkhgfg d')
    > c=db.cursor()
    > c.execute('sele ct person from persons order by person')
    > tup=c.fetchall( )
    > for a in tup:
    > print a
    > ############### ############### ############### ###
    > ausführe, erhalte ich Dinge wie
    > ('Dieter',)
    > ('Hulda',)
    > ....
    >
    > Ich brauche die Klammern, Apostrphe und Kommas nicht (ich will nur die
    > Inhalte) und kann ich sie nicht loswerden. Versucht habe ich u. a. print
    > a[2:-3] was nur zwei Klammern bringt.
    > b=lstrip(a, "(") haut auch nicht hin.
    > Weiss jemand Rat?[/color]

    Hab gerade kein MySQL da, aber änder mal

    for a in tup:
    print a

    in

    for a in tup:
    print a[0] # Das erste Element des Tupels


    Dann wird statt des ganzen Tupels nur das erste Element ausgegeben.

    [color=blue]
    > P.S. Woher kommen diese Klammern und Apostrophen, vom MySQLdb?[/color]

    Die Klammern und Apostrophe kommen daher, dass tup ein Tupel
    (http://docs.python.org/lib/typesseq.html) mit einem Element
    (der Person) ist.


    Dennis

    Comment

    • Lajos Kuljo

      #3
      Re: MySQLdb - Tuples

      Dennis Benzinger wrote:[color=blue]
      > Lajos Kuljo wrote:
      >[color=green]
      >>Hallo,
      >>ich bin voll neu im Python-Programming, deshalb ist mein Problem
      >>wahrscheinlic h trivial:
      >>
      >>Wenn ich die Script
      >>############# ############### #############33
      >>#! /usr/bin/env python
      >>import MySQLdb
      >>db=MySQLdb.co nnect(host='loc alhost', db='photum_0_6_ 2', user='root',
      >>passwd='thkhg fgd')
      >>c=db.cursor ()
      >>c.execute('se lect person from persons order by person')
      >>tup=c.fetchal l()
      >>for a in tup:
      >> print a
      >>############# ############### ############### #####
      >>ausführe, erhalte ich Dinge wie
      >>('Dieter',)
      >>('Hulda',)
      >>....
      >>
      >>Ich brauche die Klammern, Apostrphe und Kommas nicht (ich will nur die
      >>Inhalte) und kann ich sie nicht loswerden. Versucht habe ich u. a. print
      >>a[2:-3] was nur zwei Klammern bringt.
      >>b=lstrip(a, "(") haut auch nicht hin.
      >>Weiss jemand Rat?[/color]
      >
      >
      > Hab gerade kein MySQL da, aber änder mal
      >
      > for a in tup:
      > print a
      >
      > in
      >
      > for a in tup:
      > print a[0] # Das erste Element des Tupels
      >
      >
      > Dann wird statt des ganzen Tupels nur das erste Element ausgegeben.
      >
      >
      >[color=green]
      >>P.S. Woher kommen diese Klammern und Apostrophen, vom MySQLdb?[/color]
      >
      >
      > Die Klammern und Apostrophe kommen daher, dass tup ein Tupel
      > (http://docs.python.org/lib/typesseq.html) mit einem Element
      > (der Person) ist.
      >
      >
      > Dennis[/color]
      Thank you Dennis,
      it works!
      Sorry for the wrong language. I'm getting older.
      Kind regards Lajos

      Comment

      • Andy Dustman

        #4
        Re: MySQLdb - Tuples

        ############### ############### ###########33
        #! /usr/bin/env python
        import MySQLdb
        db=MySQLdb.conn ect(host='local host', db='photum_0_6_ 2', user='root',
        passwd='thkhgfg d')
        c=db.cursor()
        c.execute('sele ct person from persons order by person')
        for (person,) in c: # or c.fetchall() (may be more portable)
        print person
        ############### ############### ############### ###

        If you return more than one column, then you don't need the parentheses
        for the tuple unpacking. MySQLdb (and all other DB API databases)
        return rows as tuples, so if you only select one column, you get a
        tuple of one item.

        (google groups eats leading spaces)

        Comment

        • Georg Brandl

          #5
          Re: MySQLdb - Tuples

          Andy Dustman wrote:[color=blue]
          > ############### ############### ###########33
          > #! /usr/bin/env python
          > import MySQLdb
          > db=MySQLdb.conn ect(host='local host', db='photum_0_6_ 2', user='root',
          > passwd='thkhgfg d')
          > c=db.cursor()
          > c.execute('sele ct person from persons order by person')
          > for (person,) in c: # or c.fetchall() (may be more portable)
          > print person
          > ############### ############### ############### ###
          >
          > If you return more than one column, then you don't need the parentheses
          > for the tuple unpacking.[/color]

          You don't need the parentheses for one element either:

          for person, in c:
          print person

          works perfectly. Note the trailing comma.

          Georg

          Comment

          Working...