本文主要是介绍python Alchemy filter中datetime类型变量比较,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
python Alchemy filter中datetime类型变量比较是直接使用>、<或者>=或者<=来比较。
python Alchemy filter中datetime类型变量比较,不能将datetime变为str再比较,否则条件无效
我的代码中,误以为要将datetime转化成str类型来比较,导致相关filter不起效果。代码如下:
id为46的update时间是 2024-01-02
print("11111111111111111111111111111")query1=StuTest.query.filter(StuTest.id==46).filter(StuTest.update>'2023')print("query1:",query1)data1=query1.first()print(data1.update)print("2222222222222222222222222222")stu2 = StuTest.query.filter(StuTest.id==46).filter(str(StuTest.update) > '2026')print("query1:",stu2)data2=stu2.first()print(str(data2.update))
以上代码运行结果:
11111111111111111111111111111 query1: SELECT stu_test.id AS stu_test_id, stu_test.stu_name AS stu_test_stu_name, stu_test.stu_age AS stu_test_stu_age, stu_test.stu_class AS stu_test_stu_class, stu_test.`update` AS stu_test_update FROM stu_test WHERE stu_test.id = %(id_1)s AND stu_test.`update` > %(update_1)s 结果1: 2024-01-02 14:29:33 2222222222222222222222222222 query1: SELECT stu_test.id AS stu_test_id, stu_test.stu_name AS stu_test_stu_name, stu_test.stu_age AS stu_test_stu_age, stu_test.stu_class AS stu_test_stu_class, stu_test.`update` AS stu_test_update FROM stu_test WHERE stu_test.id = %(id_1)s 结果2: 2024-01-02 14:29:33
可以看到:加了str类型转换后的datetime条件过滤在sql语句中是无效条件。
这篇关于python Alchemy filter中datetime类型变量比较的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!