Python MySQL DB
Question: I have 4 tables in a Mysql Db named Employee. The structure of the tables are
as follows:

Edetails(**_id,name,age_**)  
Edepartment(**_id,name,dept_**)  
Edesignation(**_id,name,desig_**)  
Esalary(**_id,name,basic,pf_**)

id is the primary key in all the tables. My question is when a user gives the
id of an employee how can i fetch **_id,name,age,basic,pf_** all at a time?? I
have created all tables and added entries using python. for fetching i used
the below code.

    
    
    import MySQLdb as mdb  
    con = mdb.connect('localhost', 'root', 'root', 'Employee')  
    cur=con.cursor()  
    x=raw_input("Enter Employee id:")  
    cur.execute("Select * from Edepartment,Edesignation,Esalary,Edetails where dept.id=%s",(id))    
    res=cur.fetchall()  
    print res
    

Thanks in advance..


Answer: The `fetchall()` method gives you all rows in a set. Even if the result set
contains one record, you still have to do something like the following

    
    
    import MySQLdb as mdb  
    con = mdb.connect('localhost', 'root', 'root', 'Employee')  
    cur=con.cursor()  
    x=raw_input("Enter Employee id:")  
    cur.execute("Select table1.id, table2.name from table1,table2 where dept.id=%s",(id))    
    
    results=cur.fetchall()
    for result in results:
        print 'id = %s' % result[0]
        print 'name = %s' % result[1]
    

