如何使用 to_date 处理 python 3.5 中的无效令牌错误?
how to deal with invalid token error in python 3.5 with to_date?
我了解到 python 3.x 不接受 01、02 等值。那么,如何处理这个问题,尤其是在对 SQL 查询使用 To_date()
时。
下面是我的查询
cur.execute('select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00', 'DD-MON-YYYY hh24:mi')')
我收到以下错误
File "<ipython-input-34-c9e7408a33cb>", line 2
cur.execute('select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00', 'DD-MON-YYYY hh24:mi')')
SyntaxError: invalid token
数据库- Oracle
使用的库 - cx_Oracle
'01-Feb-2016 00:00' 是一个字符串,Python 不应该关心它包含什么。更重要的是,它是更大字符串的一部分,所以 Python 甚至不应该看到它。
我认为问题在于您编写 SQL 语句的方式:您对整个字符串和嵌入的文字使用了相同的引号。尝试改用双引号:
"select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00', 'DD-MON-YYYY hh24:mi')"
"what difference does it make with single quote and double quote?"
这些报价由 Python 处理:
"select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00','DD-MON-YYYY hh24:mi')"
^ ^
对于Python来说,就是一个字符串。
这些报价由 Oracle 处理:
"select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00','DD-MON-YYYY hh24:mi')"
^ ^ ^ ^
它们允许 Oracle 数据库区分语句中的文字。
" Ideally both should be same to represent strings, right?"
在一定程度上。从Python编译器的角度看你的原始版本:
'select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00','DD-MON-YYYY hh24:mi')'
^ ^
start of string end of string
引用成对。因此,匹配引号之后的任何内容都由 Python 处理为 Not A String。这就是为什么 Python 可以在字符串中看到 01
。
我了解到 python 3.x 不接受 01、02 等值。那么,如何处理这个问题,尤其是在对 SQL 查询使用 To_date()
时。
下面是我的查询
cur.execute('select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00', 'DD-MON-YYYY hh24:mi')')
我收到以下错误
File "<ipython-input-34-c9e7408a33cb>", line 2
cur.execute('select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00', 'DD-MON-YYYY hh24:mi')')
SyntaxError: invalid token
数据库- Oracle
使用的库 - cx_Oracle
'01-Feb-2016 00:00' 是一个字符串,Python 不应该关心它包含什么。更重要的是,它是更大字符串的一部分,所以 Python 甚至不应该看到它。
我认为问题在于您编写 SQL 语句的方式:您对整个字符串和嵌入的文字使用了相同的引号。尝试改用双引号:
"select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00', 'DD-MON-YYYY hh24:mi')"
"what difference does it make with single quote and double quote?"
这些报价由 Python 处理:
"select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00','DD-MON-YYYY hh24:mi')"
^ ^
对于Python来说,就是一个字符串。
这些报价由 Oracle 处理:
"select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00','DD-MON-YYYY hh24:mi')"
^ ^ ^ ^
它们允许 Oracle 数据库区分语句中的文字。
" Ideally both should be same to represent strings, right?"
在一定程度上。从Python编译器的角度看你的原始版本:
'select * from COREDEP where PDATE >= To_Date('01-Feb-2016 00:00','DD-MON-YYYY hh24:mi')'
^ ^
start of string end of string
引用成对。因此,匹配引号之后的任何内容都由 Python 处理为 Not A String。这就是为什么 Python 可以在字符串中看到 01
。