在 Bottle 和 MySQLdb 中执行 Int、Decimal 或 Datetime 类型不起作用

Execution of Int, Decimal or Datetime Types in Bottle and MySQLdb Not Working

我在 PythonAnywhere.com

上使用 Bottle 中的 MySQLdb 时遇到一个非常奇怪的错误

只要执行包含 int、decimal 或 datetime 类型的列,执行就会抛出 500 错误。例如,来自 table 工具,制造商和 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

但是从同一个 table 中请求 tool_id,它是 int 类型,或者从那个 table 中请求任何其他 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() 中,但它失败了。

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

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

When called by the server, the application object must return an iterable yielding zero or more strings.

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

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