0

I'm currently confused on this one. limit by default is none, but if i were to test this in unitest and ask a specific number of comments it would say type mismatch. I know that "int" is not the correct term. I'm only saying that the limit should be an integer. What would be the proper way of addressing the input depending on what the limit is in the unitest.

def list_comments(db, limit=None): limit = "int"
    limit = "int"

    cur = db.cursor()
    cur.execute("SELECT id, comment FROM comments ORDER BY id DESC LIMIT ?", (limit,))
    results = cur.fetchall()

    print results

    return results
4

1 回答 1

0

从函数后面删除limit = "int",并将limit参数默认为0并使用占位符 as %s;

def list_comments(db, limit=0):
    cur = db.cursor()
    cur.execute("SELECT id, comment FROM comments ORDER BY id DESC LIMIT %s", (str(limit)))
    results = cur.fetchall()

    print results

    return results

如果还不需要逗号,也请删除它;(limit,).

我故意将限制值解析为字符串(如果它真的是整数,您可以最初检查它)以使其始终与%s占位符匹配。

于 2013-04-18T10:22:15.063 回答