Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select query in pymysql

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!

like image 747
Jasmine078 Avatar asked Feb 15 '15 20:02

Jasmine078


People also ask

What does PyMySQL fetchAll return?

PyMySQL fetchAll The fetchAll method retrieves all (remaining) rows of a query result, returning them as a sequence of sequences. In the example, we retrieve all cities from the database table. This SQL statement selects all data from the cities table.

How do I print a SQL query in Python?

You can use a print('your query here') in the python script itself. As you're fetching the data from your query / sql snippet inside some variable in you python script, you can use a dbms_output. put_line('you query here') in the query / . sql file and that should do the trick.

What is the difference between MySQL and PyMySQL?

PyMySQL and MySQLdb provide the same functionality - they are both database connectors. The difference is in the implementation where MySQLdb is a C extension and PyMySQL is pure Python. There are a few reasons to try PyMySQL: it might be easier to get running on some systems.


1 Answers

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.

like image 149
Daniel Roseman Avatar answered Oct 02 '22 15:10

Daniel Roseman