Skip to content

Commit 32d2b99

Browse files
Backport PR #57311 on branch 2.2.x (Fixing multi method for to_sql for non-oracle databases) (#57466)
Backport PR #57311: Fixing multi method for to_sql for non-oracle databases Co-authored-by: Samuel Chai <[email protected]>
1 parent 5550bdb commit 32d2b99

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Fixed regressions
3131
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
3232
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
3333
- Fixed regression in :meth:`DataFrame.to_json` converting nullable integers to floats (:issue:`57224`)
34+
- Fixed regression in :meth:`DataFrame.to_sql` when ``method="multi"`` is passed and the dialect type is not Oracle (:issue:`57310`)
3435
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
3536
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
3637
- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)

pandas/core/generic.py

+3
Original file line numberDiff line numberDiff line change
@@ -2969,6 +2969,9 @@ def to_sql(
29692969
database. Otherwise, the datetimes will be stored as timezone unaware
29702970
timestamps local to the original timezone.
29712971
2972+
Not all datastores support ``method="multi"``. Oracle, for example,
2973+
does not support multi-value insert.
2974+
29722975
References
29732976
----------
29742977
.. [1] https://docs.sqlalchemy.org

pandas/io/sql.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1012,22 +1012,19 @@ def _execute_insert(self, conn, keys: list[str], data_iter) -> int:
10121012

10131013
def _execute_insert_multi(self, conn, keys: list[str], data_iter) -> int:
10141014
"""
1015-
Alternative to _execute_insert for DBs support multivalue INSERT.
1015+
Alternative to _execute_insert for DBs support multi-value INSERT.
10161016
10171017
Note: multi-value insert is usually faster for analytics DBs
10181018
and tables containing a few columns
10191019
but performance degrades quickly with increase of columns.
1020+
10201021
"""
10211022

10221023
from sqlalchemy import insert
10231024

10241025
data = [dict(zip(keys, row)) for row in data_iter]
1025-
stmt = insert(self.table)
1026-
# conn.execute is used here to ensure compatibility with Oracle.
1027-
# Using stmt.values(data) would produce a multi row insert that
1028-
# isn't supported by Oracle.
1029-
# see: https://docs.sqlalchemy.org/en/20/core/dml.html#sqlalchemy.sql.expression.Insert.values
1030-
result = conn.execute(stmt, data)
1026+
stmt = insert(self.table).values(data)
1027+
result = conn.execute(stmt)
10311028
return result.rowcount
10321029

10331030
def insert_data(self) -> tuple[list[str], list[np.ndarray]]:

0 commit comments

Comments
 (0)