0

I have a tuple with a single value that's the result of a database query (it gives me the max ID # currently in the database). I need to add 1 to the value to utilize for my subsequent query to create a new profile associated with the next ID #.

Having trouble converting the tuple into an integer so that I can add 1 (tried the roundabout way here by turning the values into a string and then turning into a int). Help, please.

sql = """
SELECT id
FROM profiles
ORDER BY id DESC
LIMIT 1
"""

cursor.execute(sql)
results = cursor.fetchall()
maxID = int(','.join(str(results)))
newID = maxID + 1
1
  • To get the first (AKA only) element of a 1-element tuple, do mytuple[0] ;) Commented Oct 26, 2014 at 3:26

1 Answer 1

1

If you are expecting just the one row, then use cursor.fetchone() instead of fetchall() and simply index into the one row that that method returns:

cursor.execute(sql)
row = cursor.fetchone()
newID = row[0] + 1

Rather than use an ORDER BY, you can ask the database directly for the maximum value:

sql = """SELECT MAX(id) FROM profiles"""
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.