unix_admin777
asked on
RE: Python Newbie with a Python/MYSQL Question
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:
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:
['id', 'name', 'dept', ]
((100L, 'Thomas', 'Sales'), (200L, 'Jason', 'Technology'), (300L, 'Mayla', 'Technology',), (400L, 'Nisha', 'Marketing'), (500L, 'Randy', 'Technology'))
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'))
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all of the help. The explanations were excellent as well.
In 100L the L simply means that the long integer type is used by Python. You should not care about it. The long integer and the "normal" integer were merged in newer versions of Python. Think about it as about implementation detail.
He is right also with the list/tuple answer. The cfields = [i[0] for i in cursor.description] means that you are assigning the result of so called list comprehension to the cfields.
The equivalent code without using the list comprehension is:
Open in new window
To dig deeper, the i[0] for i in cursor.description is a generator expression that returns iterator. The iterator goes through all values of i[0] where i goes through all values from another iterator returned by the cursor.description.
Try cfields = tuple([i[0] for i in cursor.description) instead (i.e. feed the tuple constructor with the results of the same generator expression instead of enclosing in the square brackets). In other words, the square brackets in the list comprehension could be viewed as a syntactic sugar for the cfields = list([i[0] for i in cursor.description)
A side note, the cursor.execute("SELECT * FROM" + ' ' + table) can be a bit simplified as cursor.execute('SELECT * FROM ' + table). Single quotes and double quotes have exactly the same meaning -- you can choose. There is no char type in Python 'x' is not a character like in the C language. It is only a single-letter string.