Reputation: 2393
From the source of to_sql, I can see that it gets mapped to an Meta Data object meta = MetaData(con, schema=schema)
. However, I can't find SQLAlchemy docs that tell me how to define the Schema for MySQL
How do I specify the schema string ?
Upvotes: 40
Views: 44672
Reputation: 535
DataFrame.to_sql(self, name, con, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None, method=None)
Just use schema parameter. But note that schema is not odbc driver.
Upvotes: 2
Reputation: 336
The schema parameter in to_sql
is confusing as the word "schema" means something different from the general meaning of "table definitions". In some SQL flavors, notably postgresql, a schema is effectively a namespace for a set of tables.
For example, you might have two schemas, one called test
and one called prod
. Each might contain a table called user_rankings
generated in pandas and written using the to_sql
command. You would specify the test
schema when working on improvements to user rankings. When you are ready to deploy the new rankings, you would write to the prod
schema.
As others have mentioned, when you call to_sql
the table definition is generated from the type information for each column in the dataframe. If the table already exists in the database with exactly the same structure, you can use the append
option to add new data to the table.
Upvotes: 13
Reputation: 76982
Starting from the Dialects page of the SQLAlchemy documentation, select documentation page of your dialect and search for create_engine
to find example on how to create it.
Even more concise overview you can get on Engine Configuration page for all supported dialects.
Verbatim extract for mysql
:
# default
engine = create_engine('mysql://scott:tiger@localhost/foo')
# mysql-python
engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')
# MySQL-connector-python
engine = create_engine('mysql+mysqlconnector://scott:tiger@localhost/foo')
# OurSQL
engine = create_engine('mysql+oursql://scott:tiger@localhost/foo')
Then pass this engine
to the to_sql(...)
of pandas' DataFrame.
Upvotes: -8