英文:
Get table description from Python sqlalchemy connection object and table name as a string
问题
从Python中的sqlalchemy
连接对象和一个表名(字符串)开始,我该如何获取表格属性,例如列名和数据类型。
例如,在sqlalchemy
中连接到一个数据库:
from sqlalchemy import create_engine
conn = create_engine('mssql+pyodbc://...driver=ODBC+Driver+17+for+SQL+Server').connect()
然后,conn
是一个sqlalchemy
连接对象:
In [1]: conn
Out[1]: <sqlalchemy.engine.base.Connection at 0x7feb6efef070>
我如何根据表名(字符串)获取表格属性,例如 table = '...'
?
这个应该可以工作,但实际上创建了一个空的DataFrame:
from sqlalchemy import text
import pandas as pd
query = f"""SELECT * FROM information_schema.columns WHERE table_name='{table}'"""
df = pd.read_sql_query(text(query), conn)
In [2]: df
Out[2]:
Empty DataFrame
Columns: [TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, NUMERIC_SCALE, DATETIME_PRECISION, CHARACTER_SET_CATALOG, CHARACTER_SET_SCHEMA, CHARACTER_SET_NAME, COLLATION_CATALOG, COLLATION_SCHEMA, COLLATION_NAME, DOMAIN_CATALOG, DOMAIN_SCHEMA, DOMAIN_NAME]
Index: []
versions:
sqlalchemy - 2.0.4
pandas - 1.5.3
英文:
Starting from a sqlalchemy
connection object in Python and a table name as a string how do I get table properties, eg column names and datatypes.
For example, connect to a database in sqlalchemy
from sqlalchemy import create_engine
conn = create_engine('mssql+pyodbc://...driver=ODBC+Driver+17+for+SQL+Server').connect()
Then conn
is a sqlalchemy
connection object
In [1]: conn
Out[1]: <sqlalchemy.engine.base.Connection at 0x7feb6efef070>
How do I get table properties based on a table name as a string, eg table = '...'
?
This should work but instead creates an empty DataFrame
from sqlalchemy import text
import pandas as pd
query = f"""SELECT * FROM information_schema.columns WHERE table_name='{table}'"""
df = pd.read_sql_query(text(query), conn)
In [2]: df
Out[2]:
Empty DataFrame
Columns: [TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, NUMERIC_SCALE, DATETIME_PRECISION, CHARACTER_SET_CATALOG, CHARACTER_SET_SCHEMA, CHARACTER_SET_NAME, COLLATION_CATALOG, COLLATION_SCHEMA, COLLATION_NAME, DOMAIN_CATALOG, DOMAIN_SCHEMA, DOMAIN_NAME]
Index: []
versions:
sqlalchemy - 2.0.4
pandas - 1.5.3
答案1
得分: 1
# 你写了
conn = create_engine(...)
建议改成
engine = create_engine(...)
从关系数据库动态加载细节(使用 sqlalchemy 1.4):
from pprint import pp
import sqlalchemy as sa
engine = ...
meta = sa.MetaData(bind=engine)
my_table = sa.Table('my_table', meta, autoload=True)
for column in my_table.c:
print(column)
查看 pp(dir(my_table))
以查看许多
其他可用的细节。
同时也可以使用 help(my_table)
。
此外,生成的表对象是会话
SELECT 查询、INSERT 等的非常方便的起点。
编辑
SqlAlchemy 2.0 是一个重大的变化。
文档
解释了你应该使用这个替代关键字:
meta = sa.MetaData()
my_table = sa.Table('my_table', meta, autoload_with=engine)
<details>
<summary>英文:</summary>
You wrote
conn = create_engine(...)
Prefer to phrase it as
engine = create_engine(...)
----
Load details dynamically from the RDBMS (with sqlalchemy 1.4):
from pprint import pp
import sqlachemy as sa
engine = ...
meta = sa.MetaData(bind=engine)
my_table = sa.Table('my_table', meta, autoload=True)
for column in my_table.c:
print(column)
Take a look at `pp(dir(my_table))` to see lots of
other details that are available.
Also `help(my_table)`.
Additionally, the resulting table object is a very
convenient starting place for session
SELECT queries, INSERTs, and so on.
----
EDIT
SqlAlchemy 2.0 is a breaking change.
The (https://docs.sqlalchemy.org/en/20/tutorial/metadata.html)
explain that you should use this alternate keyword:
meta = sa.MetaData()
my_table = sa.Table('my_table', meta, autoload_with=engine)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论