3

I have a simple dataframe like this

df = pd.DataFrame({"A":[1, 2, 3], "B":["a", "b", "c"]})

I would like to write this dataframe to vertica database using to_sql method. So I use vertica_python module and my code is the following

import pandas as pd
import vertica_python

cxn = {"user":'myuser',
       "password":'mypassword',
       "host":'xx.x.x.xx',
       "port":yyyy,
       "database":"mydb"}

engine = vertica_python.connect(**cxn)

df = pd.DataFrame({"A":[1, 2, 3], "B":["a", "b", "c"]})

df.to_sql("df", index=False, if_exists="replace", con=engine, schema="public", dtype={"A":"int", "B":"int"})

Then i got database error which i could not fix it as follows.

DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting

May I have your suggestions how to solve this problem? Thank you very much.

1
  • You have given type of B column as int convert it to object i.e., as str column Commented Jan 28, 2019 at 5:34

1 Answer 1

1

I bumped into a similar problem connecting to Vertica and managed to create a connection via the following:

  1. Using sqlalchemy and specifically sqlalchemy-vertica

As you seem to be using vertica_python, I would recommend installing with:

pip install sqlalchemy-vertica[vertica-python]

This way you can connect the following way:

import sqlalchemy as sa
import vertica_python

engine = sa.create_engine('vertica+vertica_python://user:pwd@host:port/database')

Note that according to this thread, the upload with pandas and sqlalchemy can become a lot faster due to a recent improvement in sqlalchemy by passing the following configuration to create_engine:

engine = create_engine(sqlalchemy_url, fast_executemany=True)

I haven't tried it yet, but it looks promising. More on it on that answer.

EDIT

I tried the fast_executemany flag above and unfortunately it does not work with Vertica.

Sign up to request clarification or add additional context in comments.

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.