Search code examples
pythonpandassqlalchemyvertica

Unable to write data to Vertica Database using Python SqlAlchemy - Type "TEXT" does not exist


I am trying to upload pandas dataframe into Vertica Database was able to setup the engine and query database using sqlalchemy.

But when I try to upload data from pandas dataframe get error message as Type "TEXT" does not exist. I am using windows 10, and created an ODBC connection.

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)

the above code (df.to_sql) snippet comes up with an error as : ProgrammingError: (pyodbc.ProgrammingError) ('42704', '[42704] ERROR 5108: Type "TEXT" does not exist\n (5108) (SQLExecDirectW)')

Can Anyone help on this,

Thanks in Advance !!


Solution

  • Have faced similar thing at work, and have changed types using VARCHAR for the columns which are of string object

    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