Reputation: 128
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 !!
Upvotes: 2
Views: 1663
Reputation: 41
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 !
Upvotes: 1
Reputation: 36
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
Upvotes: 2