MySQL Forums
Forum List  »  Connector/Python

Python Connector gives incorrect result
Posted by: Jim Brännlund
Date: April 07, 2024 06:43PM

This python code:

```python
import mysql.connector

try:
conn = mysql.connector.connect(
host="localhost", user="fanw", password="secret", database="fanw"
)

cursor = conn.cursor()
query = """
CREATE TEMPORARY TABLE IF NOT EXISTS temp_pairs AS

SELECT
fd.nid,
fd.title
FROM
node__field_card_display cl
JOIN
node_field_data fd ON cl.entity_id = fd.nid AND cl.langcode = fd.langcode
WHERE
cl.field_card_display_target_id = 2049
AND cl.langcode = 'en'
AND cl.bundle = 'person';

SELECT
t.entity_id,
t.deleted,
t.delta,
t.langcode,
t.body_value,
GROUP_CONCAT(DISTINCT tp.nid SEPARATOR ',') AS nids,
GROUP_CONCAT(DISTINCT tp.title SEPARATOR ',') AS titles
FROM
node__body AS t
JOIN
temp_pairs AS tp ON BINARY t.body_value LIKE CONCAT('%', tp.title, '%')
AND NOT (t.body_value LIKE CONCAT('%', tp.nid, '">', tp.title, '<', '%'))
WHERE
t.body_format <> 'plain_text'
AND t.entity_id <> tp.nid
GROUP BY
t.entity_id, t.langcode;
"""
cursor.execute(query, multi=True)
result = cursor.fetchall()
print(len(result))
conn.commit()

except Exception as e:
conn.rollback()
print(f"Error: {str(e)}")

finally:
cursor.close()
conn.close()

print("DONE!")
```

Gives the result: 0

While running the same query directly in mysql gives: 51

```sh
$ cat python_query.sql
CREATE TEMPORARY TABLE IF NOT EXISTS temp_pairs AS

SELECT
fd.nid,
fd.title
FROM
node__field_card_display cl
JOIN
node_field_data fd ON cl.entity_id = fd.nid AND cl.langcode = fd.langcode
WHERE
cl.field_card_display_target_id = 2049
AND cl.langcode = 'en'
AND cl.bundle = 'person';

SELECT
t.entity_id,
t.deleted,
t.delta,
t.langcode,
t.body_value,
GROUP_CONCAT(DISTINCT tp.nid SEPARATOR ',') AS nids,
GROUP_CONCAT(DISTINCT tp.title SEPARATOR ',') AS titles
FROM
node__body AS t
JOIN
temp_pairs AS tp ON BINARY t.body_value LIKE CONCAT('%', tp.title, '%')
AND NOT (t.body_value LIKE CONCAT('%', tp.nid, '">', tp.title, '<', '%'))
WHERE
t.body_format <> 'plain_text'
AND t.entity_id <> tp.nid
GROUP BY
t.entity_id, t.langcode;

$ mysql -u fanw -p fanw < python_query.sql | wc -l
Enter password:
52 (first row are the column names)
```

```sh
$ pip freeze | grep mysql
mysql-connector-python==8.3.0
```

python version is 3.9.10

And I'm running this on a MacBook Pro M1 (12.7).

I've also run the query using DBeaver, and it gives the same result; 51.

Options: ReplyQuote


Subject
Written By
Posted
Python Connector gives incorrect result
April 07, 2024 06:43PM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.