30 lines
773 B
Python
Executable File
30 lines
773 B
Python
Executable File
import pandas as pd
|
|
import psycopg2
|
|
|
|
# Connect to the database
|
|
conn = psycopg2.connect(
|
|
host="hostname",
|
|
database="dbname",
|
|
user="username",
|
|
password="password"
|
|
)
|
|
|
|
# Create a cursor object
|
|
cur = conn.cursor()
|
|
|
|
# Read the Excel file into a DataFrame
|
|
df = pd.read_excel('file.xlsx')
|
|
|
|
# Loop through each row of the DataFrame
|
|
for index, row in df.iterrows():
|
|
# Build the INSERT statement
|
|
query = "INSERT INTO table_name (column1, column2, column3) VALUES (%s, %s, %s)" % (row['column1'], row['column2'], row['column3'])
|
|
# Execute the INSERT statement
|
|
# (assuming you have a cursor object `cur` already established)
|
|
cur.execute(query)
|
|
#commit the transaction
|
|
conn.commit()
|
|
|
|
# Close the cursor and connection
|
|
cur.close()
|
|
conn.close() |