Skip to main content
 首页 » 编程设计

python之"Execute Python Script"模块 :Common table expressions is not supported in sqlite3 中的 Azure ML

2024年10月24日29artech

我昨天遇到了这个问题,同时尝试使用我在 Azure ML 中的“应用 SQL 转换”模块中使用的相同 sqlite 脚本,在 Azure ML 中的 Sqlite over Python 模块中:

with tbl as (select * from t1) 
select * from tbl 

这是我得到的错误:

[Critical]     Error: Error 0085: The following error occurred during script evaluation, please view the output log for more information: 
---------- Start of error message from Python interpreter ---------- 
  File "C:\server\invokepy.py", line 169, in batch 
data:text/plain,Caught exception while executing function: Traceback (most recent call last): 
    odfs = mod.azureml_main(*idfs) 
  File "C:\pyhome\lib\site-packages\pandas\io\sql.py", line 388, in read_sql 
  File "C:\temp\azuremod.py", line 193, in azureml_main 
    results = pd.read_sql(query,con) 
    coerce_float=coerce_float, parse_dates=parse_dates) 
  File "C:\pyhome\lib\site-packages\pandas\io\sql.py", line 1017, in execute 
  File "C:\pyhome\lib\site-packages\pandas\io\sql.py", line 1022, in read_sql 
    cursor = self.execute(*args) 
    raise_with_traceback(ex) 
  File "C:\pyhome\lib\site-packages\pandas\io\sql.py", line 1006, in execute 
---------- End of error message from Python  interpreter  ---------- 
    cur.execute(*args) 
DatabaseError: Execution failed on sql:  with tbl as (select * from t1) 
                    select * from tbl 

和 Python 代码:

def azureml_main(dataframe1 = None, dataframe2 = None): 
    import pandas as pd 
    import sqlite3 as lite 
    import sys 
    con = lite.connect('data1.db') 
    con.text_factory = str 
    with con: 
        cur = con.cursor() 
 
        if (dataframe1 is not None): 
            cur.execute("DROP TABLE IF EXISTS t1") 
            dataframe1.to_sql('t1',con) 
        query = '''with tbl as (select * from t1) 
                    select * from tbl'''                       
        results = pd.read_sql(query,con)     
 
    return results, 

将查询替换为:

select * from t1 

它按预期工作。 您可能知道,公用表表达式是 Sqlite 中的一个关键特性,运行递归代码的能力是任何函数式语言(例如 Sqlite)中的“必备”。

我还尝试在 Azure 的 Jupyter Notebook 中运行我的 Python 脚本,它也按预期运行。

Python 模块中的 Sqlite 配置是否可能与 Jupyter Notebook 和“应用 SQL 转换”模块中的配置不同?

请您参考如下方法:

我重现了您的问题并查看了 SQL Queries pandas.io.sql 的文档在 http://pandas.pydata.org/pandas-docs/stable/io.html#sql-queries .我尝试使用 read_sql_query解决它,但失败了。

根据pandas文档,tt 似乎是 Pandas不支持使用此 SQL 语法。

根据我的经验并根据您的SQL,我尝试执行SQL select * from (select * from t1) as tbl而不是适用于 Pandas 的 SQL .

希望对您有所帮助。此致。