Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using COPY from cursor to upload tables into the db #31

Open
alephcero opened this issue Mar 21, 2023 · 0 comments
Open

Try using COPY from cursor to upload tables into the db #31

alephcero opened this issue Mar 21, 2023 · 0 comments

Comments

@alephcero
Copy link
Collaborator

def load_dataframe_with_copy(df):
    # Create a cursor object
    cur = conn.cursor()

    # Convert the DataFrame to a CSV file-like object
    csv_buffer = io.StringIO()
    df.to_csv(csv_buffer, index=False, header=False)

    # Use the COPY command to load the CSV file into the table
    csv_buffer.seek(0)
    cur.copy_from(csv_buffer, table_name, sep=",")
    conn.commit()

    # Close the cursor and connection
    cur.close()

df = pd.read_csv(csv_file_path)

# Do data processing on df

load_dataframe_with_copy(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant