将sqlite3.cursor转换为int PYTHON

时间:2015-05-18 03:09:46

标签: python python-3.x sqlite bottle

当我尝试这个

时,我试图从我的数据库中的特定表返回一个INT'id'
retrieveID = "SELECT id FROM posts WHERE usernick=?"
latestPost = cursor.execute(retrieveID,(usernick, ))
if latestPost:
    return latestPost
else:
    return None

我回来了

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

我正在尝试针对网络测试进行测试。

我也试过了

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

但是我无法将光标转换为in。我想知道是否有办法这样做,如果没有办法解决这个问题

1 个答案:

答案 0 :(得分:3)

你必须先从光标中获取一个元素, e.g。

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

或如果该查询有很多行

,请使用cursor.fetchall()
cursor.execute(...)
for row in cursor.fetchall():
    post_id = row[0]