【问题标题】:Execution of Int, Decimal or Datetime Types in Bottle and MySQLdb Not Working在 Bottle 和 MySQLdb 中执行 Int、Decimal 或 Datetime 类型不起作用
【发布时间】:2023-03-08 14:48:01
【问题描述】:

我在 PythonAnywhere.com 上的 Bottle 中使用 MySQLdb 时遇到一个非常奇怪的错误

只要执行包含 int、decimal 或 datetime 类型的列,执行就会引发 500 错误。例如,从表 Tools 中,manufacturer 和 sub_type 都是 varchars,所以这段代码可以完美运行:

reservation_id = request.forms.get("reservation_id")
db = MySQLdb.connect("...")
c = db.cursor()
c.execute("""SELECT manufacturer, sub_type FROM Tool""")
row = c.fetchone()
return row

但是从同一个表中请求 tool_id,它是 int 类型,或者从该表中获取任何其他 int、decimal 或 datetime 类型的列:

reservation_id = request.forms.get("reservation_id")
db = MySQLdb.connect("...")
c = db.cursor()
c.execute("""SELECT tool_id FROM Tool""")
row = c.fetchone()
return row

Bottle 抛出一个严重错误:

运行 WSGI 应用程序时出错 SystemError:返回带有错误集的结果

在 MySQL bash 中,每个查询都能完美运行。将相同的查询(仅 int、decimal 或 datetime)放在 execute() 中,它会失败。

有没有人有过类似的经历?

【问题讨论】:

  • 如果您在问题中包含堆栈跟踪和 Bottle 框架代码,会更容易提供帮助,但我会尝试一下,因为我有预感。

标签: python mysql bottle pythonanywhere


【解决方案1】:

您是否返回 row 作为您的 WSGI 响应?如果是这样,那就是问题所在。根据the WSGI standard

当被服务器调用时,应用程序对象必须返回一个产生零个或多个字符串的可迭代对象。

为了说明这一点,请先尝试将您的行转换为字符串并确认这会有所帮助。例如,

...  # your existing code
return [str(col) for col in row]

【讨论】:

  • 你太棒了!做到了,谢谢!如果我们相遇,我欠你一杯咖啡!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多