我创建了一个带有学生表的数据库
myStr1 = "CREATE TABLE Students (reg_no VARCHAR(15), name VARCHAR(30), email VARCHAR(30), average_mark INTEGER, PRIMARY KEY(reg_no))"
self.myCursor.execute(myStr1)
然后,我添加了一个函数,将学生详细信息添加到表中,并希望根据条件过滤掉某些记录。以下是我的代码:
def insert_student(self,reg_no,name,email,average_mark):
self.reg_no = reg_no
self.name = name
self.email = email
self.average_mark = average_mark
self.myCursor.execute("INSERT INTO Students VALUES (:reg_no, :name, :email, :average_mark)",{'reg_no':self.reg_no,'name':self.name,'email':self.email,'average_mark':self.average_mark})
self.myConnection.commit()
def get_students(self,criteria = None):
self.criteria = criteria
self.myCursor.execute("SELECT * FROM Students WHERE '{}'".format(self.criteria))
return self.myCursor.fetchall()
myWrapper = DBWrapper()
myWrapper.insert_student('F17/42770/2017','Mutheu Lorraine','mutheu@gmail.com',100)
myWrapper.insert_student('F17/3030/2015','Ron Weasley', 'ron@eie.com',71)
myWrapper.insert_student('F17/3031/2015','Hermione Granger', 'hermine@eie.com',71)
myWrapper.insert_student('F17/3031/2017','Ginny Weasley', 'ginny@eie.com',71)
myWrapper.myConnection.commit()
students = myWrapper.get_students('reg_no = F17/42770/2017')
print(students)
然而,当我运行我的代码时,我只会得到' []'而不是具有给定注册号的学生的所有细节。我应该怎么做呢?
答案 0 :(得分:0)
格式化后,生成的SQL命令为:
output=output[['Patient_ID','Physician','Hospital']]
整个WHERE子句是一个字符串,并且该字符串对于任何行都不会计算为true。
要使条件成为SQL表达式,请删除引号。但必须引用比较值,以便将其识别为字符串:
SELECT * FROM Students WHERE 'reg_no = F17/42770/2017'