繁体   English   中英

适用于 MySQL 工作台,但不适用于 Python? 两个相同的代码。 但其他价值

[英]Works on MySQL Workbench, but not on Python? two identical codes. But other value

此 select 在 Workbench 和 Python 中工作:

#!/usr/bin/python3
import mysql.connector

mydb = mysql.connector.connect(
  host="127.0.0.1",
  user="root",
  password="xxxxxxxx",
  database="gnucash"
)

sqlcursor = mydb.cursor()
sqlcursor.execute("""
SELECT  MAX(transactions.num) AS nr , MAX(transactions.enter_date) AS enter, MAX(transactions.post_date) AS post, MAX(transactions.description) AS "beschr", SUM(splits.value_num) AS "Euro"
FROM gnucash.splits
INNER JOIN gnucash.transactions ON gnucash.splits.tx_guid = gnucash.transactions.guid
INNER JOIN gnucash.accounts ON gnucash.splits.account_guid = gnucash.accounts.guid
WHERE transactions.guid IN (
    SELECT transactions.guid FROM gnucash.splits
    INNER JOIN gnucash.transactions ON gnucash.splits.tx_guid = gnucash.transactions.guid
    INNER JOIN gnucash.accounts ON gnucash.splits.account_guid = gnucash.accounts.guid
    WHERE accounts.guid LIKE "f2dd1f1e92cf41f687187d1e73fbc2c9")
AND accounts.guid NOT LIKE "f2dd1f1e92cf41f687187d1e73fbc2c9"
GROUP BY transactions.enter_date
ORDER BY post DESC, nr DESC, enter DESC
LIMIT 30;
""")

rohumsaetze = sqlcursor.fetchall()

print(rohumsaetze)

这个 select 可以在 Workbench 上工作,但不能在 Python 上工作???

SELECT  MAX(transactions.num) AS nr , MAX(transactions.enter_date) AS enter, MAX(transactions.post_date) AS post, MAX(transactions.description) AS "beschr", SUM(splits.value_num) AS "Euro"
FROM gnucash.splits
INNER JOIN gnucash.transactions ON gnucash.splits.tx_guid = gnucash.transactions.guid
INNER JOIN gnucash.accounts ON gnucash.splits.account_guid = gnucash.accounts.guid
WHERE transactions.guid IN (
    SELECT transactions.guid FROM gnucash.splits
    INNER JOIN gnucash.transactions ON gnucash.splits.tx_guid = gnucash.transactions.guid
    INNER JOIN gnucash.accounts ON gnucash.splits.account_guid = gnucash.accounts.guid
    WHERE accounts.guid LIKE "1df66c60180c4f3cb5cc080c1e7d4834")
AND accounts.guid NOT LIKE "1df66c60180c4f3cb5cc080c1e7d4834"
GROUP BY transactions.enter_date
ORDER BY post DESC, nr DESC, enter DESC
LIMIT 30;

只有“%Sparda%”和“%Commerz%”不同。 在 Workbench 上工作正常,但我需要 python。 我已经尝试使用 root 来制作声明, 这里如何。 但没有成功。 最重要的是没有错误。 如何找到错误? 找出错误?

我已经将代码重写了 3 次。 也许有人知道如何以不同的方式编写它? 它的工作原理。

谢谢你

您的问题可能与 Sparda 的“%S”有关,在 python 中,它可能被解析为字符串占位符

我有答案。 这与“ import mysql.connector ”有关

我尝试使用相同的代码,但使用其他连接器“ import pymysql ”。 而且有效,不知道为什么。 但现在 SQL 语句可以进行选择。

#!/usr/bin/python3
import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='dariokoceic', passwd='XXXXXXXX', db='gnucash')

cur = conn.cursor()

cur.execute("""
SELECT  MAX(transactions.num) AS nr , transactions.enter_date AS enter, MAX(transactions.post_date) AS post, MAX(transactions.description) AS "beschr", SUM(splits.value_num) AS "Euro", MAX(splits.quantity_denom) 
FROM gnucash.splits
INNER JOIN gnucash.transactions ON gnucash.splits.tx_guid = gnucash.transactions.guid
INNER JOIN gnucash.accounts ON gnucash.splits.account_guid = gnucash.accounts.guid
WHERE transactions.guid IN (
    SELECT transactions.guid FROM gnucash.splits
    INNER JOIN gnucash.transactions ON gnucash.splits.tx_guid = gnucash.transactions.guid
    INNER JOIN gnucash.accounts ON gnucash.splits.account_guid = gnucash.accounts.guid
    WHERE accounts.guid LIKE "1df66c60180c4f3cb5cc080c1e7d4834")
AND accounts.guid NOT LIKE "1df66c60180c4f3cb5cc080c1e7d4834"
GROUP BY transactions.enter_date
ORDER BY post DESC, nr DESC, enter DESC
LIMIT 30;
""")

print(cur.description)

for row in cur:
    print(row)

cur.close()
conn.close()

更新。 我发现了问题。 mysql.connector 想要在使用 GROUP BY 时具有完全相同的列,必须首先在 SELECT 上。 代码应该是这样的:

#!/usr/bin/python3
import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='dariokoceic', passwd='XXXXXXXX', db='gnucash')

cur = conn.cursor()

cur.execute("""
SELECT transactions.enter_date AS enter, MAX(transactions.num) AS nr, MAX(transactions.post_date) AS post, MAX(transactions.description) AS "beschr", SUM(splits.value_num) AS "Euro", MAX(splits.quantity_denom) 
FROM gnucash.splits
INNER JOIN gnucash.transactions ON gnucash.splits.tx_guid = gnucash.transactions.guid
INNER JOIN gnucash.accounts ON gnucash.splits.account_guid = gnucash.accounts.guid
WHERE transactions.guid IN (
    SELECT transactions.guid FROM gnucash.splits
    INNER JOIN gnucash.transactions ON gnucash.splits.tx_guid = gnucash.transactions.guid
    INNER JOIN gnucash.accounts ON gnucash.splits.account_guid = gnucash.accounts.guid
    WHERE accounts.guid LIKE "1df66c60180c4f3cb5cc080c1e7d4834")
AND accounts.guid NOT LIKE "1df66c60180c4f3cb5cc080c1e7d4834"
GROUP BY transactions.enter_date
ORDER BY post DESC, nr DESC, enter DESC
LIMIT 30;
""")

print(cur.description)

for row in cur:
    print(row)

cur.close()
conn.close()

我只换了这句话...

SELECT transactions.enter_date AS enter, MAX(transactions.num) AS nr,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM