22
33
44import importlib .util
5+ import inspect
56import logging
67from typing import Any , Callable , Dict , Iterator , List , Optional , Tuple , TypeVar , Union
78
1213from awswrangler import _data_types
1314from awswrangler import _databases as _db_utils
1415from awswrangler import exceptions
16+ from awswrangler ._config import apply_configs
1517
1618__all__ = ["connect" , "read_sql_query" , "read_sql_table" , "to_sql" ]
1719
@@ -32,6 +34,9 @@ def inner(*args: Any, **kwargs: Any) -> Any:
3234 )
3335 return func (* args , ** kwargs )
3436
37+ inner .__doc__ = func .__doc__
38+ inner .__name__ = func .__name__
39+ inner .__setattr__ ("__signature__" , inspect .signature (func )) # pylint: disable=no-member
3540 return inner # type: ignore
3641
3742
@@ -281,6 +286,7 @@ def read_sql_table(
281286
282287
283288@_check_for_pyodbc
289+ @apply_configs
284290def to_sql (
285291 df : pd .DataFrame ,
286292 con : "pyodbc.Connection" ,
@@ -291,6 +297,7 @@ def to_sql(
291297 dtype : Optional [Dict [str , str ]] = None ,
292298 varchar_lengths : Optional [Dict [str , int ]] = None ,
293299 use_column_names : bool = False ,
300+ chunksize : int = 200 ,
294301) -> None :
295302 """Write records stored in a DataFrame into Microsoft SQL Server.
296303
@@ -319,6 +326,8 @@ def to_sql(
319326 If set to True, will use the column names of the DataFrame for generating the INSERT SQL Query.
320327 E.g. If the DataFrame has two columns `col1` and `col3` and `use_column_names` is True, data will only be
321328 inserted into the database columns `col1` and `col3`.
329+ chunksize: int
330+ Number of rows which are inserted with each SQL query. Defaults to inserting 200 rows per query.
322331
323332 Returns
324333 -------
@@ -357,15 +366,18 @@ def to_sql(
357366 )
358367 if index :
359368 df .reset_index (level = df .index .names , inplace = True )
360- placeholders : str = ", " .join (["?" ] * len (df .columns ))
369+ column_placeholders : str = ", " .join (["?" ] * len (df .columns ))
361370 table_identifier = _get_table_identifier (schema , table )
362371 insertion_columns = ""
363372 if use_column_names :
364373 insertion_columns = f"({ ', ' .join (df .columns )} )"
365- sql : str = f"INSERT INTO { table_identifier } { insertion_columns } VALUES ({ placeholders } )"
366- _logger .debug ("sql: %s" , sql )
367- parameters : List [List [Any ]] = _db_utils .extract_parameters (df = df )
368- cursor .executemany (sql , parameters )
374+ placeholder_parameter_pair_generator = _db_utils .generate_placeholder_parameter_pairs (
375+ df = df , column_placeholders = column_placeholders , chunksize = chunksize
376+ )
377+ for placeholders , parameters in placeholder_parameter_pair_generator :
378+ sql : str = f"INSERT INTO { table_identifier } { insertion_columns } VALUES { placeholders } "
379+ _logger .debug ("sql: %s" , sql )
380+ cursor .executemany (sql , (parameters ,))
369381 con .commit ()
370382 except Exception as ex :
371383 con .rollback ()
0 commit comments