在 Pandas to_sql 中指定架构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30994707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specifying the schema in Pandas to_sql
提问by HackToHell
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
从to_sql的来源,我可以看到它被映射到一个元数据对象meta = MetaData(con, schema=schema)。但是,我找不到告诉我如何为 MySQL 定义架构的 SQLAlchemy 文档
How do I specify the schema string ?
如何指定架构字符串?
回答by Vasin Yuriy
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.
只需使用架构参数。但请注意,架构不是 odbc 驱动程序。
回答by Eilif Mikkelsen
The schema parameter in to_sqlis 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.
模式参数 into_sql令人困惑,因为“模式”一词的含义与“表定义”的一般含义不同。在某些 SQL 风格中,特别是 postgresql,模式实际上是一组表的命名空间。
For example, you might have two schemas, one called testand one called prod. Each might contain a table called user_rankingsgenerated in pandas and written using the to_sqlcommand. You would specify the testschema when working on improvements to user rankings. When you are ready to deploy the new rankings, you would write to the prodschema.
例如,您可能有两种模式,一种称为test,一种称为prod。每个都可能包含一个user_rankings在 Pandas 中生成的表,并使用该to_sql命令编写。您将test在改进用户排名时指定架构。当您准备好部署新排名时,您将写入prod架构。
As others have mentioned, when you call to_sqlthe 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 appendoption to add new data to the table.
正如其他人所提到的,当您调用to_sql表定义时,是根据数据框中每一列的类型信息生成的。如果数据库中已经存在具有完全相同结构的表,您可以使用该append选项向表中添加新数据。
回答by van
Starting from the Dialectspage of the SQLAlchemy documentation, select documentation page of your dialect and search for create_engineto find example on how to create it.
从SQLAlchemy 文档的方言页面开始,选择您方言的文档页面并搜索create_engine以查找有关如何创建它的示例。
Even more concise overview you can get on Engine Configurationpage for all supported dialects.
您可以在所有支持的方言的引擎配置页面上获得更简洁的概述。
Verbatim extract for mysql:
逐字提取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 engineto the to_sql(...)of pandas' DataFrame.
然后将其传递engine给to_sql(...)Pandas 的 DataFrame。

