troubleshooting Question

RE: Python Newbie with a Python/MYSQL Question

Avatar of unix_admin777
unix_admin777Flag for Afghanistan asked on
ProgrammingPythonScripting Languages
4 Comments2 Solutions467 ViewsLast Modified:
Hi,
   I'm a Python newbie.  I have a pretty basic script that does a "select * from tablename" and returns two list of tuples.  The format of my TEST database looks like this:

mysql> select * from employee;
+-----+--------+------------+--------+
| id  | name   | dept       |
+-----+--------+------------+--------+
| 100 | Thomas | Sales      |   
| 200 | Jason  | Technology |  
| 300 | Mayla  | Technology |  

I want to have the column match the first record in the row.  For example, my first line would look like
id: 100 name: Thomas Dept: Sales
id:200 name: Jason  Dept: Techology
id: 300 name: Mayla Dept: Technology

Here's the last bock of my code with output:
         cursor.execute("SELECT * FROM" + ' ' + table)

         data1=cursor.fetchall()

         cfields=[i[0] for i in cursor.description]

         return cfields,data1

    except:

         print "Error: unable to fetch data"

    db.close()

def ColumnFields():


    return cfields


def main():

    test1,test2=ConnectDb()

    print test1
    print test2
     Can someone please give me ideas on how to do this.  Also, for some reason, when I print this out, I get an extra letter in my output for the second tuple.  Why is this (the L shouldn't be there with the numbers)?  Also, why is my first output a list and not a tuple? Thanks in advance!!

['id', 'name', 'dept', ]
((100L, 'Thomas', 'Sales'), (200L, 'Jason', 'Technology'), (300L, 'Mayla', 'Technology',), (400L, 'Nisha', 'Marketing'), (500L, 'Randy', 'Technology'))
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros