返回数据库中的部门类

返回数据库中的部门类

我想我做这个功能的想法是对的,但我不知道为什么
我测试时出现这个错误。有人能帮我修一下吗?

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

最佳答案

使用.execute时,需要将参数作为列表或元组传递,下面是示例

# This is the qmark style:
cur.execute("insert into people values (?, ?)", (who, age))

关于python - 返回数据库中的部门类(class),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47624566/

10-11 02:51