@@ -1902,33 +1902,108 @@ def to_sql(self, name, con, schema=None, if_exists='fail', index=True,
19021902 """
19031903 Write records stored in a DataFrame to a SQL database.
19041904
1905+ Databases supported by SQLAlchemy [1]_ are supported. Tables can be
1906+ newly created, appended to, or overwritten.
1907+
19051908 Parameters
19061909 ----------
19071910 name : string
1908- Name of SQL table
1909- con : SQLAlchemy engine or DBAPI2 connection (legacy mode)
1911+ Name of SQL table.
1912+ con : sqlalchemy. engine.Engine or sqlite3.Connection
19101913 Using SQLAlchemy makes it possible to use any DB supported by that
1911- library. If a DBAPI2 object, only sqlite3 is supported .
1912- schema : string, default None
1914+ library. Legacy support is provided for sqlite3.Connection objects .
1915+ schema : string, optional
19131916 Specify the schema (if database flavor supports this). If None, use
19141917 default schema.
19151918 if_exists : {'fail', 'replace', 'append'}, default 'fail'
1916- - fail: If table exists, do nothing.
1917- - replace: If table exists, drop it, recreate it, and insert data.
1918- - append: If table exists, insert data. Create if does not exist.
1919+ How to behave if the table already exists.
1920+
1921+ * fail: Raise a ValueError.
1922+ * replace: Drop the table before inserting new values.
1923+ * append: Insert new values to the existing table.
1924+
19191925 index : boolean, default True
1920- Write DataFrame index as a column.
1926+ Write DataFrame index as a column. Uses `index_label` as the column
1927+ name in the table.
19211928 index_label : string or sequence, default None
19221929 Column label for index column(s). If None is given (default) and
19231930 `index` is True, then the index names are used.
19241931 A sequence should be given if the DataFrame uses MultiIndex.
1925- chunksize : int, default None
1926- If not None, then rows will be written in batches of this size at a
1927- time. If None, all rows will be written at once.
1928- dtype : dict of column name to SQL type, default None
1929- Optional specifying the datatype for columns. The SQL type should
1930- be a SQLAlchemy type, or a string for sqlite3 fallback connection.
1932+ chunksize : int, optional
1933+ Rows will be written in batches of this size at a time. By default,
1934+ all rows will be written at once.
1935+ dtype : dict, optional
1936+ Specifying the datatype for columns. The keys should be the column
1937+ names and the values should be the SQLAlchemy types or strings for
1938+ the sqlite3 legacy mode.
1939+
1940+ Raises
1941+ ------
1942+ ValueError
1943+ When the table already exists and `if_exists` is 'fail' (the
1944+ default).
1945+
1946+ See Also
1947+ --------
1948+ pandas.read_sql : read a DataFrame from a table
1949+
1950+ References
1951+ ----------
1952+ .. [1] http://docs.sqlalchemy.org
1953+ .. [2] https://www.python.org/dev/peps/pep-0249/
1954+
1955+ Examples
1956+ --------
1957+
1958+ Create an in-memory SQLite database.
1959+
1960+ >>> from sqlalchemy import create_engine
1961+ >>> engine = create_engine('sqlite://', echo=False)
1962+
1963+ Create a table from scratch with 3 rows.
1964+
1965+ >>> df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']})
1966+ >>> df
1967+ name
1968+ 0 User 1
1969+ 1 User 2
1970+ 2 User 3
1971+
1972+ >>> df.to_sql('users', con=engine)
1973+ >>> engine.execute("SELECT * FROM users").fetchall()
1974+ [(0, 'User 1'), (1, 'User 2'), (2, 'User 3')]
1975+
1976+ >>> df1 = pd.DataFrame({'name' : ['User 4', 'User 5']})
1977+ >>> df1.to_sql('users', con=engine, if_exists='append')
1978+ >>> engine.execute("SELECT * FROM users").fetchall()
1979+ [(0, 'User 1'), (1, 'User 2'), (2, 'User 3'),
1980+ (0, 'User 4'), (1, 'User 5')]
1981+
1982+ Overwrite the table with just ``df1``.
1983+
1984+ >>> df1.to_sql('users', con=engine, if_exists='replace',
1985+ ... index_label='id')
1986+ >>> engine.execute("SELECT * FROM users").fetchall()
1987+ [(0, 'User 4'), (1, 'User 5')]
1988+
1989+ Specify the dtype (especially useful for integers with missing values).
1990+ Notice that while pandas is forced to store the data as floating point,
1991+ the database supports nullable integers. When fetching the data with
1992+ Python, we get back integer scalars.
1993+
1994+ >>> df = pd.DataFrame({"A": [1, None, 2]})
1995+ >>> df
1996+ A
1997+ 0 1.0
1998+ 1 NaN
1999+ 2 2.0
2000+
2001+ >>> from sqlalchemy.types import Integer
2002+ >>> df.to_sql('integers', con=engine, index=False,
2003+ ... dtype={"A": Integer()})
19312004
2005+ >>> engine.execute("SELECT * FROM integers").fetchall()
2006+ [(1,), (None,), (2,)]
19322007 """
19332008 from pandas .io import sql
19342009 sql .to_sql (self , name , con , schema = schema , if_exists = if_exists ,
0 commit comments