MySQLdb - Tuples

L

Lajos Kuljo

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.connect(host='localhost', db='photum_0_6_2', user='root',
passwd='thkhgfgd')
c=db.cursor()
c.execute('select 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?
 
D

Dennis Benzinger

Lajos said:
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.connect(host='localhost', db='photum_0_6_2', user='root',
passwd='thkhgfgd')
c=db.cursor()
c.execute('select 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?

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.

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

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


Dennis
 
L

Lajos Kuljo

Dennis said:
Lajos said:
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.connect(host='localhost', db='photum_0_6_2', user='root',
passwd='thkhgfgd')
c=db.cursor()
c.execute('select 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?


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.


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


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


Dennis
Thank you Dennis,
it works!
Sorry for the wrong language. I'm getting older.
Kind regards Lajos
 
A

Andy Dustman

#########################################33
#! /usr/bin/env python
import MySQLdb
db=MySQLdb.connect(host='localhost', db='photum_0_6_2', user='root',
passwd='thkhgfgd')
c=db.cursor()
c.execute('select 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)
 
G

Georg Brandl

Andy said:
#########################################33
#! /usr/bin/env python
import MySQLdb
db=MySQLdb.connect(host='localhost', db='photum_0_6_2', user='root',
passwd='thkhgfgd')
c=db.cursor()
c.execute('select 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.

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

for person, in c:
print person

works perfectly. Note the trailing comma.

Georg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top