python - 将 sqlite3.cursor 转换为 int PYTHON

标签 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。我想知道是否有办法做到这一点,如果没有,我该如何解决这个问题

最佳答案

你必须先从光标处获取一个元素, 例如

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/

相关文章:

C++ MFC SQLite sqlite3_exec 回调

python - 如何在 Python 中从数据库创建 CSV 文件?

python - Django + OsX Lion + 带有 XAMPP 的 Mysql

Python 使用 Pandas 对多列进行分组和聚合

python - 关闭并重新打开Python套接字后为"[Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted"

python - 我用字典将值分配给字符串列表。我如何对这些值求和?

mysql - 如何从 python/kivy 应用程序存储数据

python - pandas hub_table 获取列和行中的平均值

python-3.x - Scrapy - 类型错误 : 'Rule' object is not iterable

android - 如何将 AlarmManager 与存储在 SQLite 数据库中的数据一起使用?