pandas 1.4.2

NotesParametersReturns
to_sql(frame, name: 'str', con, schema: 'str | None' = None, if_exists: 'str' = 'fail', index: 'bool' = True, index_label=None, chunksize: 'int | None' = None, dtype: 'DtypeArg | None' = None, method: 'str | None' = None, engine: 'str' = 'auto', **engine_kwargs) -> 'int | None'

Notes

The returned rows affected is the sum of the rowcount attribute of sqlite3.Cursor or SQLAlchemy connectable. The returned value may not reflect the exact number of written rows as stipulated in the sqlite3 or SQLAlchemy

Parameters

frame : DataFrame, Series
name : str

Name of SQL table.

con : SQLAlchemy connectable(engine/connection) or database string URI

or sqlite3 DBAPI2 connection Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported.

schema : str, optional

Name of SQL schema in database to write to (if database flavor supports this). If None, use default schema (default).

if_exists : {'fail', 'replace', 'append'}, default 'fail'
  • fail: If table exists, do nothing.

  • replace: If table exists, drop it, recreate it, and insert data.

  • append: If table exists, insert data. Create if does not exist.

index : bool, default True

Write DataFrame index as a column.

index_label : str or sequence, optional

Column label for index column(s). If None is given (default) and :None:None:`index` is True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex.

chunksize : int, optional

Specify the number of rows in each batch to be written at a time. By default, all rows will be written at once.

dtype : dict or scalar, optional

Specifying the datatype for columns. If a dictionary is used, the keys should be the column names and the values should be the SQLAlchemy types or strings for the sqlite3 fallback mode. If a scalar is provided, it will be applied to all columns.

method : {None, 'multi', callable}, optional

Controls the SQL insertion clause used:

engine : {'auto', 'sqlalchemy'}, default 'auto'

SQL engine library to use. If 'auto', then the option io.sql.engine is used. The default io.sql.engine behavior is 'sqlalchemy'

versionadded
**engine_kwargs :

Any additional kwargs are passed to the engine.

Returns

None or int

Number of rows affected by to_sql. None is returned if the callable passed into method does not return the number of rows.

versionadded

Write records stored in a DataFrame to a SQL database.

Examples

See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/io/sql.py#602
type: <class 'function'>
Commit: