【问题标题】:Azure ML in "Execute Python Script" module :Common table expressions is not supported in sqlite3“执行 Python 脚本”模块中的 Azure ML:sqlite3 不支持公用表表达式
【发布时间】:2026-02-05 19:45:02
【问题描述】:

我昨天遇到了这个问题,同时尝试使用我在 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 转换”模块中的配置不同?

【问题讨论】:

标签: python sqlite common-table-expression azure-machine-learning-studio cortana-intelligence


【解决方案1】:

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

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

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

希望对您有所帮助。最好的祝福。

【讨论】:

    最近更新 更多