Search code examples
pythonpython-3.xsqlitebottle

Converting sqlite3.cursor to int PYTHON


I am trying to return an INT 'id' from a particular table in my database when i try this

retrieveID = "SELECT id FROM posts WHERE usernick=?"
latestPost = cursor.execute(retrieveID,(usernick, ))
if latestPost:
    return latestPost
else:
    return None

I return

AssertionError: <class 'int'> != <class 'sqlite3.Cursor'> error

I am trying to test it against a webtest.

I have also tried the

int(cursor.execute(retrieveID, (usernick,))

But I am unable to convert the cursor to an in. I was wondering if there is a way to do so and if there isn't how do I go about fixing this issue


Solution

  • You have to get an element from the cursor first, e.g.

    cursor.execute(...)
    post_id = cursor.fetchone()[0]
    

    or use cursor.fetchall() if there are many rows for that query

    cursor.execute(...)
    for row in cursor.fetchall():
        post_id = row[0]