Python Forum

Full Version: get last row id in pymysql
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am pretty new to python
I am wondering is this is the correct syntax to get the last row id in a my=sql mysql table?
cur=conn.cursor()
sql="SELECT row_id FROM test_tb ORDER BY row_id DESC LIMIT 1"
cur.execute(sql)
last_id = cur.fetchone()
for lastID in last_id:
    print(lastID)
Thanks.
You can do it with your query:
SELECT * FROM Table ORDER BY ID DESC LIMIT 1
you have it (almost)  right, so there will only be one row, and it's the one you want.