【问题标题】:Converting sqlite3.cursor to int PYTHON将 sqlite3.cursor 转换为 int PYTHON
【发布时间】:2015-07-29 10:40:34
【问题描述】:

当我尝试此操作时,我试图从我的数据库中的特定表中返回一个 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

我正在尝试针对 webtest 对其进行测试。

我也试过了

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

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

【问题讨论】:

标签: python python-3.x sqlite bottle


【解决方案1】:

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

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

如果该查询有很多行,则使用 cursor.fetchall()

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

【讨论】:

  • 非常感谢 :) 你能回答我一件事吗? "cursor.fetchone()[0]" '[0]' 代表什么?
  • 您可以将其视为包含许多列的一行,因此您正在读取查询返回的行的第一列(您的查询仅返回一列)
猜你喜欢
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 2019-06-18
  • 2015-10-09
相关资源
最近更新 更多