ValueError:操作参数必须是str(从数据库中删除记录)

时间:2017-03-07 12:52:37

标签: python sqlite

我正在尝试创建一个可以从我创建的数据库中删除学生的功能。

def remove_students():
        cursor = db.cursor()
        first_name = input("Enter first name: ")
        surname = input("Enter surname:  ")
        year_group = int(input("Enter year group: "))
        student_info = (first_name, surname, year_group)
        cursor.execute(student_info, remove_student)
        db.commit()

sql_2 = """CREATE TABLE students(
        ID SERIAL PRIMARY KEY,
        first_name TEXT,
        Surname TEXT,
        year_group INTEGER,
        strike INTEGER,);"""

remove_student = "DELETE FROM students (first_name, surname, year_group) values (?,?,?)"

当我运行此代码时,我收到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "M:\computer science a2\comp 3\login.py", line 112, in remove_students
    cursor.execute(student_info, remove_student)
ValueError: operation parameter must be str

我不明白,因为我在年份组的参赛作品之前添加了int。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

错误在cursor.execute(student_info, remove_student)

应该是cursor.execute(remove_student,student_info)

这是修改后的方法!

def remove_students():
    cursor = db.cursor()
    first_name = input("Enter first name: ")
    surname = input("Enter surname:  ")
    year_group = int(input("Enter year group: "))
    student_info = (first_name, surname, year_group)
    #cursor.execute(sql_query,params)
    cursor.execute(remove_student, student_info)
    db.commit()

希望它有所帮助!

答案 1 :(得分:0)

您写道:

cursor.execute(student_info, remove_student)

你的功能。我认为应该是:

cursor.execute(remove_student, student_info)

“operation”是第一个位置参数(你有第二个)。