python - 如何在 if 指令中处理 SQL 选择中的 none

标签 python mysql database raspberry-pi raspberry-pi3

您好 Stackoverflow 社区,

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

我们想要记录缺勤情况。 首先,如果数据库中存在NULL,如果学生迟到了5分钟,则应将NULL替换为5分钟。如果他在类(class)结束前离开,则应添加时间。

我当前从数据库中读取 None (NULL),我想在 IF 语句中使用它。 所以 if = NULL then 第一个语句 Else 另一个。 这样做时,我总是收到以下错误。

在从数据库读取结果之前,请尝试将结果删除为 None

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

类型错误:“instancemethod”对象没有属性“getitem

最佳答案

我在 result3=c.fetchall 行上看到错误。 fetchall 是函数,因此您必须用括号调用它:fetchall()。 下一个错误是:当您执行 str(result[0][0]) 时,它不再是 bool 值,而是字符串。 因此,如果 result[0][0] 将为 None,则在使用 str(result[0][0]) 转换后,它将包含字符串“无”。所以你现在的语句就像 if ("None"== None) 一样,它总是 False。

关于python - 如何在 if 指令中处理 SQL 选择中的 none,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361448/

相关文章:

database - DROP TRIGGER 使用变量定义触发器名称

python - SQLAlchemy:加入没有 from 字段的子查询

c++ - 在 Python 中使用点云库

python - 我会遇到 python 的全局解释器锁的问题吗?

php - HTML 日期时间 到 PHP 到 MySQL

MYSQL - 根据条件更新新列的值

python - 遍历字典列表并与其他字典进行比较

php - 从开始日期相等且结束日期之间的 sql 中检索数据

php - Doctrine2 导致网站速度变慢

c# - 如何使用 STAMP 加密读取 tbf 文件