2017-03-07 51 views
0

我正在尝试创建一个可以从我创建的数据库中删除学生的函数。ValueError:操作参数必须是str(从数据库删除记录)

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。任何帮助将非常感激。

回答

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() 

希望它有帮助!

+0

我试过了,但是我得到了同样的错误信息 – toni

+0

你确定吗?你可以请分享更新后的代码作为图像在这里添加为编辑问题! –

+0

oh no ...对不起,它没有狐狸的问题,但现在我得到一个新的错误消息说:cursor.execute(remove_student,student_info) sqlite3.OperationalError:附近“(”:语法错误 – toni

0

您写道:

cursor.execute(student_info, remove_student) 

自己的函数中。我觉得应该是:

cursor.execute(remove_student, student_info) 

“操作”是第一个位置参数(你有它秒)。

+0

DB-API的参数替换使用'?'作为占位符。问题! –

相关问题