Remi-007
Remi-007

Reputation: 145

Copy Pandas Dataframe to Postgres with odo

There is this top python library odo that I would love to use to transfer data from Pandas to my postgres db . it looks so convenient but i don't manage to use it :( .

Has someone an idea what I am missing. It must be a little thing .....

So i come with the following ligne of code :

import odo, pandas 
..... 
odo(df, 'postgresql://postgres:password@localhost::mapping_ean_asin') 

I got the error:

TypeError: 'module' object is not callable

Thank you in advance for your help :)

Upvotes: 1

Views: 570

Answers (1)

Remi-007
Remi-007

Reputation: 145

Solution to my on question :) !

SQL_STATEMENT = """ COPY %s FROM STDIN WITH
    CSV
    HEADER
    DELIMITER AS ','
"""

my_file = open("/home.........csv")


def process_file(conn, table_name, file_object):
      cursor = conn.cursor()
      cursor.copy_expert(sql=SQL_STATEMENT % table_name, file=file_object)
      conn.commit()
      cursor.close()

connection =  psycopg2.connect("dbname='postgres' user='postgres' host='localhost'           password='password'")
     try:
           process_file(connection, 'my table', my_file)
     finally:
           connection.close()

I would have prefer the a solution in one line with odo but I got an second error.....

Upvotes: 0

Related Questions