473,416 Members | 1,806 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,416 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 2413
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
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?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.