从Python的SQLAlchemy连接对象和表名字符串中获取表描述。

huangapple go评论57阅读模式
英文:

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(&#39;mssql+pyodbc://...driver=ODBC+Driver+17+for+SQL+Server&#39;).connect()

Then conn is a sqlalchemy connection object

In [1]: conn
Out[1]: &lt;sqlalchemy.engine.base.Connection at 0x7feb6efef070&gt;

How do I get table properties based on a table name as a string, eg table = &#39;...&#39;?

This should work but instead creates an empty DataFrame

from sqlalchemy import text
import pandas as pd
query = f&quot;&quot;&quot;SELECT * FROM information_schema.columns WHERE table_name=&#39;{table}&#39;&quot;&quot;&quot;
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>



huangapple
  • 本文由 发表于 2023年4月7日 01:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定