I am trying to get a single cell of values from MySQLdb, in Python. Here's my code:
fname = c.execute("""SELECT fname from employees WHERE user = %s;""", (useruname))
However what I get is "1L", which is not what I want - fname should contain a string, not a long integer.
Why would it do this?
PyMySQL fetchAll The fetchall function gets all records. It returns a result set. Technically, it is a tuple of tuples. Each of the inner tuples represent a row in the table.
MySQLdb module, a popular interface with MySQL is not compatible with Python 3.
Next, db object is used to create a cursor object, which in turn is used to execute SQL queries.
The method execute
"returns long integer rows affected, if any".
To get the value of fname, you need to fetch the results using for example fetchall
or fetchone
:
cursor.execute("""SELECT fname from employees WHERE user = %s""", (useruname,))
row = cursor.fetchone()
print row[0]
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