2
2
3
3
4
4
import importlib .util
5
+ import inspect
5
6
import logging
6
7
from typing import Any , Callable , Dict , Iterator , List , Optional , Tuple , TypeVar , Union
7
8
12
13
from awswrangler import _data_types
13
14
from awswrangler import _databases as _db_utils
14
15
from awswrangler import exceptions
16
+ from awswrangler ._config import apply_configs
15
17
16
18
__all__ = ["connect" , "read_sql_query" , "read_sql_table" , "to_sql" ]
17
19
@@ -32,6 +34,9 @@ def inner(*args: Any, **kwargs: Any) -> Any:
32
34
)
33
35
return func (* args , ** kwargs )
34
36
37
+ inner .__doc__ = func .__doc__
38
+ inner .__name__ = func .__name__
39
+ inner .__setattr__ ("__signature__" , inspect .signature (func )) # pylint: disable=no-member
35
40
return inner # type: ignore
36
41
37
42
@@ -281,6 +286,7 @@ def read_sql_table(
281
286
282
287
283
288
@_check_for_pyodbc
289
+ @apply_configs
284
290
def to_sql (
285
291
df : pd .DataFrame ,
286
292
con : "pyodbc.Connection" ,
@@ -291,6 +297,7 @@ def to_sql(
291
297
dtype : Optional [Dict [str , str ]] = None ,
292
298
varchar_lengths : Optional [Dict [str , int ]] = None ,
293
299
use_column_names : bool = False ,
300
+ chunksize : int = 200 ,
294
301
) -> None :
295
302
"""Write records stored in a DataFrame into Microsoft SQL Server.
296
303
@@ -319,6 +326,8 @@ def to_sql(
319
326
If set to True, will use the column names of the DataFrame for generating the INSERT SQL Query.
320
327
E.g. If the DataFrame has two columns `col1` and `col3` and `use_column_names` is True, data will only be
321
328
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.
322
331
323
332
Returns
324
333
-------
@@ -357,15 +366,18 @@ def to_sql(
357
366
)
358
367
if index :
359
368
df .reset_index (level = df .index .names , inplace = True )
360
- placeholders : str = ", " .join (["?" ] * len (df .columns ))
369
+ column_placeholders : str = ", " .join (["?" ] * len (df .columns ))
361
370
table_identifier = _get_table_identifier (schema , table )
362
371
insertion_columns = ""
363
372
if use_column_names :
364
373
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 ,))
369
381
con .commit ()
370
382
except Exception as ex :
371
383
con .rollback ()
0 commit comments