From 716862a82463b43bfe438eb4479230c6cfc56e6f Mon Sep 17 00:00:00 2001 From: jaca Date: Sun, 26 Feb 2023 14:26:11 +0100 Subject: [PATCH 1/2] EHN: allow for to_sql `multi` method with oracle backend --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/io/sql.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 45b5c16415f9d..fc9a82d0bff2c 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -28,7 +28,7 @@ enhancement2 Other enhancements ^^^^^^^^^^^^^^^^^^ -- +- :meth:`to_sql` with method parameter set to ``multi`` works with Oracle on the backend - .. --------------------------------------------------------------------------- diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 7dedd705f8c70..5eac23e0dc0ca 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -960,8 +960,8 @@ def _execute_insert_multi(self, conn, keys: list[str], data_iter) -> int: from sqlalchemy import insert data = [dict(zip(keys, row)) for row in data_iter] - stmt = insert(self.table).values(data) - result = conn.execute(stmt) + stmt = insert(self.table) + result = conn.execute(stmt, data) return result.rowcount def insert_data(self) -> tuple[list[str], list[np.ndarray]]: From 1667b3c5febcf185f4becff9723c355414fda483 Mon Sep 17 00:00:00 2001 From: jaca Date: Tue, 10 Oct 2023 19:37:48 +0200 Subject: [PATCH 2/2] add explanation why conn.execute is used instead of insert.values --- pandas/io/sql.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 840b29632f6c3..3818950535d90 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -964,6 +964,10 @@ def _execute_insert_multi(self, conn, keys: list[str], data_iter) -> int: data = [dict(zip(keys, row)) for row in data_iter] stmt = insert(self.table) + # conn.execute is used here to ensure compatibility with Oracle. + # Using stmt.values(data) would produce a multi row insert that + # isn't supported by Oracle. + # see: https://docs.sqlalchemy.org/en/20/core/dml.html#sqlalchemy.sql.expression.Insert.values result = conn.execute(stmt, data) return result.rowcount