Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get one cell's values with MySQLdb

Tags:

python

mysql

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?

like image 425
Steven Matthews Avatar asked Nov 03 '11 20:11

Steven Matthews


People also ask

What does PyMySQL Fetchall return?

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.

Does MySQLdb work with python3?

MySQLdb module, a popular interface with MySQL is not compatible with Python 3.

Which object is used to execute an SQL query in MySQLdb in Python?

Next, db object is used to create a cursor object, which in turn is used to execute SQL queries.


1 Answers

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]
like image 59
Mark Byers Avatar answered Oct 07 '22 09:10

Mark Byers