小编Gém*_*nis的帖子

Python MySQLdb不正确的日期时间值:'2018-03-25 02:00:02'

我使用mysqlclient包的MySQLdb在Python上收到此错误.

_mysql_exceptions.OperationalError: 
(1292, "Incorrect datetime value: '2018-03-25 02:00:02' for column 'start' at row 1")
Run Code Online (Sandbox Code Playgroud)

导致错误的代码:

conn.cursor.execute(query.format(table=table), 
(row['id'], row['type'], row['start'], row['end'],       
row['raw_xml'], row['id_parent'], row['rango']))
Run Code Online (Sandbox Code Playgroud)

我可以正确插入其余行,但是当我尝试插入此datetime对象时,它会崩溃.

数据库中的字段是一个时间戳字段,它与其余的datetime对象一起工作,但不知道为什么它试图使这个语句崩溃.

提前致谢.

python mysql timezone datetime dst

3
推荐指数
1
解决办法
1333
查看次数

重新引发异常不适用于“finally”子句

当我放入finally子句时,raise语句 inexcept不起作用。

所以except块不会产生Exception.

我错过了什么?如果我想重新提高Exceptionafterfinally子句返回值,我需要做什么?

def test():
    res = 1
    try:
            raise Exception
            res = 2
    except:
            print('ha fallado')
            raise

    finally:
            return res
test()
Run Code Online (Sandbox Code Playgroud)

解决方案

def test():
    res = 1
    try:
            raise Exception
            res = 2
    except:
            print('ha fallado')
            raise

    finally:
            # ... finally code that need to exec
            pass

    return res

print(test())
Run Code Online (Sandbox Code Playgroud)

这样,如果发生异常,except 块会处理该异常,然后引发它。

如果没有发生异常,则返回该值。

感谢所有的答案!这么快 :)

python exception

2
推荐指数
1
解决办法
753
查看次数

标签 统计

python ×2

datetime ×1

dst ×1

exception ×1

mysql ×1

timezone ×1