Details
-
Bug
-
Status: Open (View Workflow)
-
Minor
-
Resolution: Unresolved
-
None
-
None
-
None
-
None
Description
The way executemany() fails depends on whether the server supports the bulk insertion protocol or not. For servers that support it, if any of the rows fails to insert, no rows are inserted. For server that do not support it, all rows that precede the error are inserted but all rows after it are not.
The following program demonstrates the problem.
#!/usr/bin/env python3
|
|
import mariadb |
|
conn = mariadb.connect( |
user="maxuser", |
password="maxpwd", |
host="127.0.0.1", |
port=4006, |
database="test" |
)
|
|
print(conn.get_server_version()) |
|
# Get Cursor
|
cur = conn.cursor() |
values = [ |
("a", ), |
("b", ), |
("c", ), |
("d", ), |
("a", ), |
("e", ), |
("f", ), |
]
|
|
cur.execute("CREATE OR REPLACE TABLE t1 (data CHAR(30) NOT NULL PRIMARY KEY)") |
|
try: |
cur.executemany("INSERT INTO t1 VALUES (?)", values) |
except Exception as e: |
print(e) |
|
cur.execute("SELECT * FROM t1") |
print([x for x in cur.fetchall()]) |
This is what when I tested it with a fake version string of 8.0.1-maxscale:
[markusjm@monolith python]$ ./bulk_execute.py
|
(8, 0, 1)
|
Duplicate entry 'a' for key 'PRIMARY'
|
[('a',), ('b',), ('c',), ('d',)]
|
[markusjm@monolith python]$ ./bulk_execute.py
|
(10, 11, 3)
|
Duplicate entry 'a' for key 'PRIMARY'
|
[]
|