gpt4 book ai didi

python - 如何使 df.to_sql() 创建 varchar2 对象

转载 作者:行者123 更新时间:2023-11-30 22:44:15 26 4
gpt4 key购买 nike

我有一个由一列字符串组成的DataFrame。如果我使用 df.to_sql() 将其作为表保存到 Oracle 数据库中,则该列是 CLOB 类型,我需要对其进行转换。我想知道在创建表时是否可以指定类型(例如 varchar2)?

最佳答案

您可以显式指定 SQLAlchemy 类型:

import cx_Oracle
from sqlalchemy import types, create_engine
engine = create_engine('oracle://user:password@host_or_scan_address:1521/ORACLE_SERVICE_NAME')

df.to_sql('table_name', engine, if_exists='replace',
dtype={'str_column': types.VARCHAR(df.str_column.str.len().max())})

df.str_column.str.len().max() - 将计算最大字符串长度

注意:对于 Oracle,types.VARCHAR 将映射到 VARCHAR2(请参阅 working example here)

关于python - 如何使 df.to_sql() 创建 varchar2 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41566950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com