【发布时间】:2017-12-03 23:39:44
【问题描述】:
我认为我有正确的想法来执行此功能,但我不确定我为什么会得到 当我测试它时出现这个错误。谁能帮我解决这个问题?
cur.execute(q)
sqlite3.ProgrammingError:提供的绑定数量不正确。这 当前语句使用 1,并且提供了 0。
当前尝试
def find_dept_courses(db, dept):
'''Return the courses from the given department. Use the "LIKE"
clause in your SQL query for the course name.'''
return run_query(db, '''SELECT DISTINCT Course FROM Courses WHERE
Course LIKE (? + 'dept%')''')
期望的输出
find_dept_courses('exams.db', 'BIO')
# [('BIOA01H3F',), ('BIOA11H3F',), ('BIOB10H3F',), ('BIOB33H3F',),
# ('BIOB34H3F',), ('BIOB50H3F',), ('BIOC12H3F',), ('BIOC15H3F',),
# ('BIOC19H3F',), ('BIOC32H3F',), ('BIOC37H3F',), ('BIOC50H3F',),
# ('BIOC58H3F',), ('BIOC59H3F',), ('BIOC61H3F',), ('BIOC63H3F',),
# ('BIOD21H3F',), ('BIOD22H3F',), ('BIOD23H3F',), ('BIOD26H3F',),
# ('BIOD33H3F',), ('BIOD48H3F',), ('BIOD65H3F',)]
查询函数:
def run_query(db, q, args=None):
"""(str, str, tuple) -> list of tuple
Return the results of running query q with arguments args on
database db."""
conn = sqlite3.connect(db)
cur = conn.cursor()
# execute the query with the given args passed
# if args is None, we have only a query
if args is None:
cur.execute(q)
else:
cur.execute(q, args)
results = cur.fetchall()
cur.close()
conn.close()
return results
【问题讨论】:
-
Sqlite 语句中的
?是一个占位符,必须用具体值填充(通常在执行语句时作为参数附加) -
好的,我该如何解决这个问题?
-
尝试删除
? +部分。这不是最好的方法,但应该可以。 -
我只是得到空列表。我没有得到想要的输出。
-
检查我的编辑。我添加了查询功能。