Python takes the first column value instead of the last one

0

I'm trying to get the last record from a column in MySQL but Python only returns "1", it's an auto_incriment ID record. I tried in several ways, in some the whole column was returned to me, in other era it returns "1". Currently the code looks like this:

d = cur.execute("SELECT MAX(Id) FROM Pings")
    
asked by anonymous 19.05.2017 / 22:57

2 answers

0

Python SQL calls in any driver are made in two parts: in one you call the "execute" method of the cursor, as you do - this call only returns the number of results found, not the results in yes - so its d always contains 1.

To actually retrieve the results of the QL query, you call a new method in Python - for example - fetchall 'which returns all of your results.

In your case, it might look like this:

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

(If it is a result only, you can use retchone as well.

    
20.05.2017 / 18:09
0

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

    
22.05.2017 / 21:52