Skip to content

Commit f8a7fe4

Browse files
kassettmroeschke
andauthored
Fixing multi method for to_sql for non-oracle databases (#57311)
* Fixing multi method for to_sql for non-oracle databases * Simplifying the if statement * adding a doc * Adding unit test * Update doc/source/whatsnew/v2.2.1.rst Co-authored-by: Matthew Roeschke <[email protected]> * Reverted formatting in test_sql * Simplifying unit test * Removing unit test * remove trailing whitespaces * Removing trailing whitespace * fixing alpahbetical sorting --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent c5bf3d3 commit f8a7fe4

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
@@ -2808,6 +2808,9 @@ def to_sql(
28082808
database. Otherwise, the datetimes will be stored as timezone unaware
28092809
timestamps local to the original timezone.
28102810
2811+
Not all datastores support ``method="multi"``. Oracle, for example,
2812+
does not support multi-value insert.
2813+
28112814
References
28122815
----------
28132816
.. [1] https://docs.sqlalchemy.org

pandas/io/sql.py

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

997997
def _execute_insert_multi(self, conn, keys: list[str], data_iter) -> int:
998998
"""
999-
Alternative to _execute_insert for DBs support multivalue INSERT.
999+
Alternative to _execute_insert for DBs support multi-value INSERT.
10001000
10011001
Note: multi-value insert is usually faster for analytics DBs
10021002
and tables containing a few columns
10031003
but performance degrades quickly with increase of columns.
1004+
10041005
"""
10051006

10061007
from sqlalchemy import insert
10071008

10081009
data = [dict(zip(keys, row)) for row in data_iter]
1009-
stmt = insert(self.table)
1010-
# conn.execute is used here to ensure compatibility with Oracle.
1011-
# Using stmt.values(data) would produce a multi row insert that
1012-
# isn't supported by Oracle.
1013-
# see: https://docs.sqlalchemy.org/en/20/core/dml.html#sqlalchemy.sql.expression.Insert.values
1014-
result = conn.execute(stmt, data)
1010+
stmt = insert(self.table).values(data)
1011+
result = conn.execute(stmt)
10151012
return result.rowcount
10161013

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

0 commit comments

Comments
 (0)