Select query in pymysql

Jasmine078 picture Jasmine078 · Feb 15, 2015 · Viewed 39.7k times · Source

When executing the following:

import pymysql 



db = pymysql.connect(host='localhost', port=3306, user='root')
cur = db.cursor()    
print(cur.execute("SELECT ParentGuardianID FROM ParentGuardianInformation WHERE UserID ='" + UserID + "'"))

The output is1

How could I alter the code so that the actual value of the ParentGuardianID (which is '001') is printed as opposed to 1.

I'm sure the answer is simple but I am a beginner so any help would be much appreciated - thanks!

Answer

Daniel Roseman picture Daniel Roseman · Feb 15, 2015

cur.execute() just returns the number of rows affected. You should do cur.fetchone() to get the actual result, or cur.fetchall() if you are expecting multiple rows.