473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.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?
Jul 18 '05 #1
4 2410
Lajos Kuljo wrote:
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
Jul 18 '05 #2
Dennis Benzinger wrote:
Lajos Kuljo wrote:
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
Jul 18 '05 #3
#########################################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)

Jul 18 '05 #4
Andy Dustman wrote:
#########################################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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Harrison | last post by:
Hi all, got a problem combinging mx and MySQLdb, when I build and install both for my Python2.1 install on a Solaris 9 box I can import mx fine, but importing MySQLdb causing python to core dump. ...
1
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
1
by: Michael | last post by:
Is there a mailing list or anything like that for users of the MySQLdb module? Using version 0.9.2 with Python 2.3.2 and whenever I try to open a connection I get this error: File...
2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
2
by: olekristianvillabo | last post by:
The method cursor.executemany is there in order to avoid multiple calls to cursor.execute(). I have tried, with success, to do like every single example (that I have found on the www) on the...
1
by: Steve | last post by:
Darwin steve.local 8.3.0 Darwin Kernel Version 8.3.0: Mon Oct 3 20:04:04 PDT 2005; root:xnu-792.6.22.obj~2/RELEASE_PPC Power Macintosh powerpc MacOSX 10.4.3 mysql Ver 14.7 Distrib 4.1.14, for...
2
by: borris | last post by:
doesn anyone know a good reference, tute or examples of MySQLdb's dictCursor I want to pass dictionaries into the sql exec statements. I could only succeed with text as values this is how I...
1
by: Yi Xing | last post by:
Hi, I met the following error when I tried to install MySQLdb. I had no problem installing numarray, Numeric, Rpy, etc. Does anyone know what's the problem? Thanks! running install running...
0
by: Steve Holden | last post by:
Vaibhav.bhawsar wrote: imported The point here is that MySQLdb is a package, not a module. Some packages have their top-level __init__.py import the package's sub-modules or sub-packages to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.