mysql_connect_python在递归函数上返回null

ltskdhd1  于 2023-04-29  发布在  Mysql
关注(0)|答案(1)|浏览(101)

在查询中,im使用递归函数生成用户创建会话的第一个日期(start_time)和当前月份之间的蒙茨。

WITH recursive Date_Ranges AS (
    SELECT (SELECT start_time FROM session WHERE user_id = 'sGcO42wMx1ZHXwZpZH3KWwWXzd13' ORDER BY start_time ASC LIMIT 1) AS d
    UNION ALL
    SELECT d + interval 1 month
    FROM Date_Ranges
    WHERE d < NOW()
)
SELECT * FROM Date_Ranges

当在ex DataGrip中执行时,返回下表:
| d|
| --------------|
| 2023-01-19 13:41:29|
| 2023-02-19 13:41:29|
| 2023-03-19 13:41:29|
| 2023-04-19 13:41:29|
| 2023-05-19 13:41:29|
但是当我通过连接器再次运行该命令时,它返回null。

consumption = DB.query("""
    WITH recursive Date_Ranges AS (
        SELECT (SELECT start_time FROM user.session WHERE user_id = 'sGcO42wMx1ZHXwZpZH3KWwWXzd13' ORDER BY start_time ASC LIMIT 1) AS d
        UNION ALL
        SELECT d + interval 1 month
        FROM Date_Ranges
        WHERE d < NOW()
    )
    SELECT * FROM Date_Ranges
""")

注意,DB.query()是一个实现,它创建一个游标,并返回fetchAll()
它看起来像是mysql_connect_python中的一个问题

编辑

我试着复制代码,没有我的DB处理程序。连接:

import os
import mysql.connector
import pprint as pprint

HOST = os.environ.get('AZURE_MYSQL_HOST')
USER = os.environ.get('AZURE_MYSQL_USER')
PASSWORD = os.environ.get('AZURE_MYSQL_PASSWORD')
DATABASE = os.environ.get('AZURE_MYSQL_NAME')
PORT = os.environ.get('AZURE_MYSQL_PORT')

query = """WITH recursive date_ranges AS (
    SELECT (SELECT start_time FROM session WHERE user_id = 'sGcO42wMx1ZHXwZpZH3KWwWXzd13' ORDER BY start_time ASC LIMIT 1) AS d
    UNION ALL
    SELECT d + interval 1 month
    FROM Date_Ranges
    WHERE d < NOW()
)
SELECT * FROM Date_Ranges"""

for use_pure in (True, False):
    with mysql.connector.connect(
        host= HOST,
        user= USER,
        passwd= PASSWORD,
        database= DATABASE,
        port= PORT,
        ssl_disabled=False,
        use_pure=use_pure
    ) as cnx:
        with cnx.cursor() as cur:
            cur.execute(query)
            res = cur.fetchall()
            print(use_pure)
            print(res)

我估计结果是:

True
[(datetime.datetime(2023, 1, 19, 13, 41, 29),), (datetime.datetime(2023, 2, 19, 13, 41, 29),), (datetime.datetime(2023, 3, 19, 13, 41, 29),), (datetime.datetime(2023, 4, 19, 13, 41, 29),), (datetime.datetime(2023, 5, 19, 13, 41, 29),)]
False
[(datetime.datetime(2023, 1, 19, 13, 41, 29),), (datetime.datetime(2023, 2, 19, 13, 41, 29),), (datetime.datetime(2023, 3, 19, 13, 41, 29),), (datetime.datetime(2023, 4, 19, 13, 41, 29),), (datetime.datetime(2023, 5, 19, 13, 41, 29),)]

所以问题一定是在我自己的插件实现。

3pmvbmvn

3pmvbmvn1#

你能提供连接器/Python代码部分,或者一个可复制的代码片段吗?
以下示例按预期工作:

import mysql.connector

stmt = """
    WITH RECURSIVE cte (n) AS
    (
    SELECT 1
    UNION ALL
    SELECT n + 1 FROM cte WHERE n < 5
    )
    SELECT * FROM cte;
"""

for use_pure in (True, False):
    with mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        use_pure=use_pure,
    ) as cnx:
        with cnx.cursor() as cur:
            cur.execute(stmt)
            res = cur.fetchall()
            print(f"Using {use_pure=}: {res=}")

结果:

Using use_pure=True: res=[(1,), (2,), (3,), (4,), (5,)]
Using use_pure=False: res=[(1,), (2,), (3,), (4,), (5,)]

相关问题