Quantcast
Channel: MySQL Forums - Connector/Python
Viewing all articles
Browse latest Browse all 384

Python Connector gives incorrect result (no replies)

$
0
0
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.

Viewing all articles
Browse latest Browse all 384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>