2

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 !!

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering, but what do you do with your updatedict then ? Trying to change dtypes with df= df.astype(updatedict) will raise an error dtype '<class 'sqlalchemy.sql.sqltypes.VARCHAR'>' not understood
1

Nice solution @calestini !

import sqlalchemy as sa 

engine = create_engine('vertica+vertica_python://user:{}@host:5433/db_name')

df.to_sql(name='table_name', schema = 'schema_name', con = engine, if_exists='append', dtype = updatedict, index = False)

This is how the datatype dictionary looks after running your function:

{'custom_index': sqlalchemy.sql.sqltypes.VARCHAR,
 'lastmodifiedbysales': sqlalchemy.sql.sqltypes.VARCHAR,
 'linked_distributor': sqlalchemy.sql.sqltypes.VARCHAR,
 'linked_reseller': sqlalchemy.sql.sqltypes.VARCHAR,
 'number_of_licenses': sqlalchemy.sql.sqltypes.VARCHAR}

Thanks !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.