如果说明

时间:2019-01-25 08:22:42

标签: python mysql database raspberry-pi raspberry-pi3

您好Stackoverflow社区,

对于一个学校项目,我目前正在Raspberry Pi 3上通过数据库连接编写Python程序。 Raspberry Pi已连接到RFID扫描仪。

我们要记录缺席情况。 首先,如果数据库中有NULL,则如果学生迟到5分钟,则应将NULL替换为5分钟。并且如果他在课程结束之前离开,则应该增加时间。

我目前从数据库中读取None(空),我想在IF语句中使用它。 因此,如果= NULL,则首先声明其他。 这样,我总是会收到以下错误。

在从数据库中读取结果之前,已经准备好尝试将结果Delcare删除为“无”。

 StudentID = str(result[0][0])
 c.execute("Select %s FROM  Attendances WHERE StudentID  = %s AND Date = %s 
 AND %s IS NULL  " % (("Std" +str((result1[0] 
 [1]))),StudentID,str((datetime.now().strftime('%Y%m%d'))),("Std" + 
 +str((result1[0][1])))))
 result3 = c.fetchall



if str(result3[0][0]) is None :



                c.execute("UPDATE Attendances SET %s = (Select current_time() - MIN(LessonStart)  from LessonTime where current_time() < LessonsEnds) WHERE SchuelerID  = %s AND Datum = %s AND %s IS NULL  " % (("Std" +str((result1[0][1]))),SchuelerID,str((datetime.now().strftime('%Y%m%d'))),("Std" +str((result1[0][1]))))),

                print (datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
                print 'Successfully registered as present'

            else:
                c.execute("UPDATE Attendances SET %s =  %s  +   (Select current_time() - MIN(LessonEnds)  from LessonTime where current_time() < LessonsEnds) WHERE StudentID  = %s AND Date = %s AND %s IS NOT NULL  " % (("Std" +str((result1[0][1]))),("Std" +str((result1[0][1]))),SchuelerID,str((datetime.now().strftime('%Y%m%d'))),("Std" +str((result1[0][1])))))

TypeError:“ instancemethod”对象没有属性“ getitem

1 个答案:

答案 0 :(得分:0)

我在第result3=c.fetchall行看到错误。 fetchall是函数,因此必须使用方括号fetchall()来调用它。 下一个错误是:执行str(result[0][0])时,它不再是布尔值,而是字符串。 因此,如果result[0][0]将是None,则在用str(result[0][0])进行转换之后,它将包含字符串"None"。因此,您的声明现在就像if ("None" == None),它将始终为False。