Search code examples
pythonmysql-connector

Wrong value of row obtained


I am using the python.mysql connector to access a MySQL table.

The MySQL database has a table policy_data which has a few columns, one being application_no.

The application_no column has values in the form of t000... totaling 8 digits including t.

Ideally, the first value of application_no column is t0000001.

So I pass a command (from Python):

cursor.execute(select* application_no from policy_data where...(some condition)
data = cursor.fetchall()
appl = data[0][0] # this should give me 't0000001'

Here's the problem: I tried the above command as it is on MySQL, and it gives me t0000001. But from Python (the above code), the value (appl=data[0][0]) is coming as t.

I even tried putting the received value inside str(), but it still doesn't work.


Solution

  • data=cursor.fetchall() returns a list of tuples (one tuple for each row of your table)

    appl=data[0][0] returns the first element of the first tuple namely the value of first column of first row in your query result.

    Given this, if column 'application_no' is second in your query result (and it is as you use * in your query) you will get the values of this column with data[i][1]

    So if you check for aapl=data[0][1] it sould return your desired output 't0000001'