当我在MySQL Workbench中运行查询时,我收到11个结果(如预期的那样),但是当我使用python-mysql.connector运行完全相同的查询时,我得到0结果。知道怎么可能吗?
我已启用query_time = 0的慢速日志来记录每个查询的详细信息。
使用MySQL Workbench进行查询:
# Time: 2016-07-24T16:09:29.154764Z
# User@Host: root[root] @ localhost [127.0.0.1] Id: 4
# Query_time: 0.007341 Lock_time: 0.000326 Rows_sent: 11 Rows_examined: 536
SET timestamp=1469376569;
SELECT rank, IF(rank>10,NULL,ccy) AS ccy, SUM(req_count)
FROM (
SELECT @rank := (@rank := @rank+1) AS rank, ccy, req_count
FROM (
SELECT want_currency AS ccy, COUNT(*) AS req_count
FROM stage.data_tracking
WHERE user_location LIKE '%Singapore%' AND
search_country = 'SG' AND
have_currency = 'SGD' AND
want_currency != 'SGD' AND
amount_usd != 1000
GROUP BY want_currency
ORDER BY req_count DESC
) AS d1
) AS d2
CROSS JOIN (SELECT @rank := 0) AS param
WHERE rank IS NOT NULL
GROUP BY LEAST(rank, 11);
的Python:
# Time: 2016-07-24T16:01:12.230648Z
# User@Host: root[root] @ localhost [127.0.0.1] Id: 11
# Query_time: 0.003171 Lock_time: 0.000188 Rows_sent: 0 Rows_examined: 514
SET timestamp=1469376072;
SELECT rank, IF(rank>10,NULL,ccy) AS ccy, SUM(req_count)
FROM (
SELECT @rank := (@rank := @rank+1) AS rank, ccy, req_count
FROM (
SELECT want_currency AS ccy, COUNT(*) AS req_count
FROM stage.data_tracking
WHERE user_location LIKE '%Singapore%' AND
search_country = 'SG' AND
have_currency = 'SGD' AND
want_currency != 'SGD' AND
amount_usd != 1000
GROUP BY want_currency
ORDER BY req_count DESC
) AS d1
) AS d2
CROSS JOIN (SELECT @rank := 0) AS param
WHERE rank IS NOT NULL
GROUP BY LEAST(rank, 11);
据我所知,这些查询是相同的,但发送的行数和检查的行数仍然不同。
我的Python代码很简单:
config = {
'user': os.environ.get("DB_USERNAME"),
'password': os.environ.get("DB_PASSWORD"),
'host': os.environ.get("DB_HOST"),
'database': os.environ.get("DB_DATABASE"),
'raise_on_warnings': True,
'buffered': True,
}
try:
conn = mysql.connector.connect(**config)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
raise
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
raise
else:
print(err)
raise
query = """
SELECT rank, IF(rank>10,NULL,ccy) AS ccy, SUM(req_count)
FROM (
SELECT @rank := (@rank := @rank+1) AS rank, ccy, req_count
FROM (
SELECT want_currency AS ccy, COUNT(*) AS req_count
FROM stage.data_tracking
WHERE user_location LIKE %s AND
search_country = %s AND
have_currency = %s AND
want_currency != %s AND
amount_usd != 1000
GROUP BY want_currency
ORDER BY req_count DESC
) AS d1
) AS d2
CROSS JOIN (SELECT @rank := 0) AS param
WHERE rank IS NOT NULL
GROUP BY LEAST(rank, 11)
"""
params = ("%Singapore%", "SG", "SGD", "SGD")
cursor = conn.cursor()
cursor.execute(query, params)
row = cursor.fetchone()
print(row)
while row is not None:
print(row)
row = cursor.fetchone()
我尝试添加/删除连接参数(如缓冲),fetchall而不是fetchone,不同的mysql用户。
我正在跑步:
更新
找到了解决方法!如果我将代码更改为:
cursor = conn.cursor()
cursor.execute(query, params)
cursor.close()
cursor = conn.cursor()
cursor.execute(query, params)
row = cursor.fetchone()
print(row)
while row is not None:
print(row)
row = cursor.fetchone()
然后会显示正确的结果!什么会导致这种行为?这会是python.mysql-connector中的错误吗?