Discuss / Python / 练习

练习

Topic source

xian_wen

#1 Created at ... [Delete] [Delete and Lock User]
def get_score_in(low, high):
    """
    返回指定分数区间的名字,按分数从低到高排序

    :param low: low score
    :param high: high score
    :return: list of names
    """
    conn = sqlite3.connect(db_file)
    cursor = conn.cursor()
    cursor.execute('SELECT name, score FROM user WHERE score BETWEEN ? AND ? ORDER BY score', (low, high))
    values = cursor.fetchall()
    return [value[0] for value in values]

xian_wen

#2 Created at ... [Delete] [Delete and Lock User]

忘记close了:

def get_score_in(low, high):
    """
    返回指定分数区间的名字,按分数从低到高排序

    :param low: low score
    :param high: high score
    :return: list of names
    """
    conn = sqlite3.connect(db_file)
    cursor = conn.cursor()
    cursor.execute('SELECT name, score FROM user WHERE score BETWEEN ? AND ? ORDER BY score', (low, high))
    values = cursor.fetchall()
    cursor.close()
    conn.close()
    return [value[0] for value in values]

  • 1

Reply