我正在尝试将 pandas 数据框上传到 Vertica 数据库
能够使用 sqlalchemy 设置引擎和查询数据库。
但是,当我尝试从 pandas dataframe 上传数据时,会收到错误消息,因为类型“TEXT”不存在。我使用的是 Windows 10,并创建了 ODBC 连接。
import sqlalchemy as sa
engine = sa.create_engine('vertica+pyodbc:///?odbc_connect=%s' %(urllib.parse.quote('DSN=TESTDB'),))
sql_query = "select * from sample_table"
df = pd.read_sql_query(sql_query, con=engine) # this works, get the data as required in the dataframe
*df.apply[Do various data transformations as required]*
# Write back to the database
df.to_sql(name='sample_table_cleaned', con = engine, schema = "Dev" , if_exists = 'append', index = True)
上面的代码(df.to_sql)片段出现错误:ProgrammingError:(pyodbc.ProgrammingError)('42704','[42704] ERROR 5108:类型“TEXT”不存在\n(5108)(SQLExecDirectW) ')
任何人都可以帮忙解决这个问题吗
提前致谢 !!
在工作中遇到过类似的事情,并且使用 VARCHAR 更改了字符串对象列的类型
def updateType(df_para):
dtypedict = {} # create and empty dictionary
for i,j in zip(df_para.columns, df_para.dtypes):
if "object" in str(j):
dtypedict.update({i: sa.types.VARCHAR})
return dtypedict
updatedict = updateType(df) # update the datafraame type
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)