当我尝试此操作时,我试图从数据库中的特定表返回 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。我想知道是否有办法做到这一点,如果没有,我该如何解决这个问题
最佳答案
你必须先从光标处获取一个元素, 例如
cursor.execute(...)
post_id = cursor.fetchone()[0]
如果该查询有很多行,则使用cursor.fetchall()
cursor.execute(...)
for row in cursor.fetchall():
post_id = row[0]
关于python - 将 sqlite3.cursor 转换为 int PYTHON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30294515/