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!
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With