在shell中调用函数时出现sqlite错误

时间:2017-11-30 03:38:44

标签: python database sqlite

我收到错误:

builtins.NameError: name 'sqlite3' is not defined

就行了

conn = sqlite3.connect(db)

有人可以帮我解决这个问题吗?

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


 def get_course_instructors(db, course):
    '''Return the Course number, sections and instructors for the given course 
      number.'''
    return (run_query(db, '''SELECT CourseNumber, sections, instructors WHERE
    course = ?''', (course)))

1 个答案:

答案 0 :(得分:0)

import sqlite3 # make sure you have this

def get_course_instructors(db, course):
'''Return the Course number, sections and instructors for the given course 
number.'''

从Courses表中获取课程代码,部分和教师,假设您的变量名称是Courses表中的“CourseNumber,sections,instructors”。

return run_query(db, '''SELECT Course, Section, Name FROM Courses WHERE course = ?''', (course,)) 

确保在return语句中有(逗号)之后的逗号。

测试用例看起来像这样。

get_course_instructors('exams.db', 'AFSA01H3F')
[('AFSA01H3F', 'LEC01', 'S. Rockel')]