Python Forum
pymysql; insert on duplicate key update fails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pymysql; insert on duplicate key update fails
#1
i dont want to go to deep here so...i want to do the above stated kind of query with pymysql...

relevant piece of code:

sql = "INSERT INTO entries (node_id, attrib_id, value, unique_id) VALUES (%d, %d, '%s', '%s') AS new \
                   ON DUPLICATE KEY UPDATE \
                   node_id = CASE WHEN new.unique_id <> unique_id THEN new.node_id ELSE node_id END, \
                   attrib_id =  CASE WHEN new.unique_id <> unique_id THEN new.attrib_id ELSE attrib_id END, \
                   value =  CASE WHEN new.unique_id <> unique_id THEN new.value ELSE value END, \
                   unique_id = new.unique_id" % \
                   (self.get_or_create_node_id(node_name), self.get_or_create_attrib_id(attrib_name), value, hash,)
        self.run_query(sql)
which results in this query string:

INSERT INTO entries (node_id, attrib_id, value, unique_id) VALUES (6688, 932, '8.0.0.636', 'e8ff77aff84da8f7755bdb9218e7a963') AS new ON DUPLICATE KEY UPDATE node_id = CASE WHEN new.unique_id <> unique_id THEN new.node_id ELSE node_id END, attrib_id =  CASE WHEN new.unique_id <> unique_id THEN new.attrib_id ELSE attrib_id END, value =  CASE WHEN new.unique_id <> unique_id THEN new.value ELSE value END, unique_id = new.unique_id;
the db refuses to do it hence its valid sql which i proofed against some sql validator

Error:
2022-06-28 12:11:47,172 [ERROR] Something went wrong during db interaction: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS new ON DUPLICATE KEY UPDATE node_id ...' at line 1")
table:
MariaDB [aix_registry_dev]> describe entries;
+-----------+--------------+------+-----+---------------------+-------------------------------+
| Field     | Type         | Null | Key | Default             | Extra                         |
+-----------+--------------+------+-----+---------------------+-------------------------------+
| id        | int(11)      | NO   | PRI | NULL                | auto_increment                |
| node_id   | int(11)      | NO   | MUL | NULL                |                               |
| attrib_id | int(11)      | NO   | MUL | NULL                |                               |
| value     | varchar(256) | NO   |     | NULL                |                               |
| unique_id | varchar(256) | NO   |     | NULL                |                               |
| ts        | timestamp    | NO   |     | current_timestamp() | on update current_timestamp() |
+-----------+--------------+------+-----+---------------------+-------------------------------+
6 rows in set (0.017 sec)

MariaDB [aix_registry_dev]> show indexes from entries;
+---------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name                | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| entries |          0 | PRIMARY                 |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| entries |          0 | ENTRIES_UNIQUE_COMPOUND |            1 | node_id     | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| entries |          0 | ENTRIES_UNIQUE_COMPOUND |            2 | attrib_id   | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| entries |          0 | ENTRIES_UNIQUE_COMPOUND |            3 | value       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| entries |          0 | ENTRIES_UNIQUE_COMPOUND |            4 | unique_id   | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| entries |          1 | id                      |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| entries |          1 | HAS_A_ATTRIBUTE         |            1 | attrib_id   | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| entries |          1 | HAS_A_NODE              |            1 | node_id     | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
8 rows in set (0.000 sec)
version
MariaDB [aix_registry_dev]> SELECT @@version;
+-----------------+
| @@version       |
+-----------------+
| 10.4.18-MariaDB |
+-----------------+
1 row in set (0.000 sec)
i am stuck, any ideas/hints?
Reply
#2
It could be nothing, it could be everything, but I don't see a semicolon terminator at the end of the query string, as there is with with the SQL statement on line 1, above the table.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
(Jun-28-2022, 10:59 AM)rob101 Wrote: It could be nothing, it could be everything, but I don't see a semicolon terminator at the end of the query string, as there is with with the SQL statement on line 1, above the table.

seems the semicolon whas eaten during copy/paste..added it back
Reply
#4
seems mariadb simply does not support this "AS" construct....mysql 8+ only feature
rob101 likes this post
Reply
#5
in line 6 shouldn't there be something (data type) after % unique_id = new.unique_id" % \ ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql Syntax error in pymysql ilknurg 4 2,345 May-18-2022, 06:50 AM
Last Post: ibreeden
  pymysql: insert query throws error wardancer84 12 4,557 Jan-28-2022, 06:48 AM
Last Post: wardancer84
  pymysql can't do SELECT * Pedroski55 3 2,960 Sep-11-2021, 10:18 PM
Last Post: Pedroski55
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,670 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  pymysql won't handle some diacritic characters awarren2001AD 0 1,262 Apr-16-2020, 08:58 AM
Last Post: awarren2001AD
  pyMySQL - ROW_NUMBER in SQL statement JayCee 1 2,374 Apr-12-2020, 08:40 PM
Last Post: JayCee
  pyMySQL How do I get the row id JayCee 3 2,703 Apr-12-2020, 08:38 PM
Last Post: JayCee
  PyMySQL return a single dictionary Valon1981 2 1,774 Feb-20-2020, 04:07 PM
Last Post: Valon1981
  pymysql: formating ouput of query wardancer84 18 8,327 Oct-04-2018, 01:54 PM
Last Post: wardancer84
  pymysql ifnull aland 4 3,982 Sep-06-2018, 05:58 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020