Python takes the first column value instead of the last one

Asked

Viewed 495 times

0

I’m trying to get the last record of a column in Mysql but Python only returns "1", is an ID record with auto_incriment. I tried several ways, in some I was returned the whole column, in other era returns "1". Currently the code is like this:

d = cur.execute("SELECT MAX(Id) FROM Pings")
  • 3

    If you run this command directly on the bench which is the result shown?

2 answers

0

d = cur.execute("SELECT Id FROM Pings ORDER BY Id DESC")

0


Python’s SQL calls, in any driver, are made in two parts: in one you call the cursor’s "run" method, as you do - this call only returns the number of results found, not the results themselves - so its variable dalways contains 1.

To actually retrieve the results of the QL query, you call a new Python method - for example - the` fetchall that returns all its results.

In your case, you can stay like this:

cur.execute("SELECT MAX(Id) FROM Pings")
d = cur.fetchall()[0]

(if it is only a result, you can use the retchone also.

  • It worked out! Thank you!

  • (if it worked, you can mark the answer as accepted)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.