Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 755
NubmcNub Avatar asked Jun 29 '26 07:06

NubmcNub


1 Answers

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]
like image 101
bakkal Avatar answered Jun 30 '26 22:06

bakkal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!