有效的' SELECT' SQL语句在Python中失败但是来自SQLite Manager

时间:2017-10-30 22:58:01

标签: python python-2.7 sqlite

此SQL语句在SQLite Manager中运行良好:

select a.etl_name, a.start
from (
select etl_name,start,end
from etl_info_is
where (etl_name, strftime('%Y-%m-%d %H:%M', start) ) in (
select etl_name, strftime('%Y-%m-%d %H:%M', start) as start
from etl_info_is
where etl_name = 'My_Value'
except
select etl_name, strftime('%Y-%m-%d %H:%M', schedule) as start
from etl_info_cron
where etl_name = 'My_Value' ) ) a join etl_info_is b
on (a.start between b.start and b.end and a.end between b.start and b.end)
where b.etl_name = 'My_Value' and
julianday('now') - julianday(a.start) <= 15

但是,同一个程序在执行Python代码时失败并出现错误:

Traceback (most recent call last):
  File "/opt/rh/python27/root/usr/lib64/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/opt/rh/python27/root/usr/lib64/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/opt/IBM/InformationServer/Server/DSEngine/bin/ISMD-jppilot.py/__main__.py", line 2241, in <module>
  File "/opt/IBM/InformationServer/Server/DSEngine/bin/ISMD-jppilot.py/__main__.py", line 1926, in fillCRON_DSchd
  File "/opt/IBM/InformationServer/Server/DSEngine/bin/ISMD-jppilot.py/__main__.py", line 1867, in get_ST_d_schd_seq
sqlite3.OperationalError: near ",": syntax error

这是失败的Python函数:

def get_ST_d_schd_seq(key_f, seq_d_f):
    """ Get the start times of the double-scheduled seq """
    conn,c = connect(sqldb_loc)

    c.execute("""select a.etl_name, a.start
    from (
    select etl_name,start,end
    from etl_info_is
    where (etl_name, strftime('%Y-%m-%d %H:%M', start) ) in (
    select etl_name, strftime('%Y-%m-%d %H:%M', start) as start
    from etl_info_is
    where etl_name = ?
    except
    select etl_name, strftime('%Y-%m-%d %H:%M', schedule) as start
    from etl_info_cron
    where etl_name = ? ) ) a join etl_info_is b
    on (a.start between b.start and b.end and a.end between b.start and b.end)
    where b.etl_name = ? and
    julianday('now') - julianday(a.start) <= 15""", (seq_d_f, seq_d_f, key_f))

    get_ST_d_schd_query = c.fetchall()
    close(conn,"Y")

    ST_d_schd_query_l = []
    for row in get_ST_d_schd_query:
        ST_d_schd = row[0]
        ST_d_schd = ST_d_schd.encode("utf-8")
        ST_d_schd = parse(ST_d_schd).strftime("%Y-%m-%d %H:%M")
        ST_d_schd_query_l.append(ST_d_schd)

    return ST_d_schd_query_l

我甚至尝试使用关于三个变量内容的硬编码值执行相同的代码 - 也尝试使用&#39; .format&#39;替换变量的方法,使它们以正常的简单方式注入。

EDIT_1:看来原因可能是因为Python 2.7.13中的SQLite版本。我们可以使SQL语句更简单,以便它可以在SQLite 3.6.20上运行吗?

EDIT_2:我已经通过RedHad软件集合安装了Python 3.5,但似乎提供了相同版本的SQLite:

Python 3.5.1 (default, Sep 15 2016, 08:33:29)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.6.20'

1 个答案:

答案 0 :(得分:1)

自SQLite 3.15.0起支持

Row values。 Python中的SQLite库较旧(在sqlite_version模块中检查sqlite3)。