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? 4 2423
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.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?
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
Dennis Benzinger wrote: Lajos Kuljo wrote:
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?
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
############### ############### ###########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)
Andy Dustman wrote: ############### ############### ###########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.
You don't need the parentheses for one element either:
for person, in c:
print person
works perfectly. Note the trailing comma.
Georg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
I am using egenenix 2.0.4 and MySQLdb 0.9.2.
I have also replaced the -shared flag in the Makefile is /usr/local/lib/Python2.1/config with -G (a...
|
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 "conflicting header files," but I don't
know what to do about conflicting header files, or where I would find
them, and once I found them, which ones to...
|
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 "/home/webserver/localhost/dvdlookup/MyMySQL.py", line 23, in
_connect
passwd = self.password, db = self.db )
File
|
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,
sometimes not.
From mysql:
mysql> show databases;
+-----------+
| Database |
|
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 subject shows, to use a insert statement
on the form:
statement = INSERT INTO table (colA,colB,colC) values (%s,%s,%s)
and pass in a list containing...
| |
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 apple-darwin8.2.0 (powerpc) using
readline 4.3
runing the software gives me
steve:~/MySQL-python-1.2.0 steve$ python setup.py build
|
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 got the cursor object to call cursor.execute("query")
connection = MySQLdb.connect( host = "localhost", user =
self.config.userName, passwd =...
|
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 build
running build_py
creating build
|
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 make them immediately available within the package
namespace (which is why, for example, you can access os.path.* when you
have imported os) and others...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |