-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,4 +131,5 @@ dist | |
.pnp.* | ||
|
||
chowkidaar | ||
./bot/__pycache__ | ||
./bot/__pycache__ | ||
list.csv |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import pandas as pd | ||
import psycopg2 | ||
from psycopg2 import Error | ||
from config import DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST | ||
from db import connect_to_database | ||
|
||
def upload_csv_to_pg(csv_file, table_name): | ||
try: | ||
# Connect to the PostgreSQL database | ||
conn = connect_to_database() | ||
|
||
# Create a cursor object using the connection | ||
cursor = conn.cursor() | ||
|
||
create_table_query = """ | ||
CREATE TABLE IF NOT EXISTS student_list_2023 ( | ||
sl INT PRIMARY KEY, | ||
student_id VARCHAR(10) NOT NULL, | ||
name VARCHAR(255) NOT NULL, | ||
gender VARCHAR(10) NOT NULL, | ||
branch VARCHAR(50) NOT NULL | ||
); | ||
""" | ||
|
||
try: | ||
cursor.execute(create_table_query) | ||
conn.commit() | ||
print("Table created successfully!") | ||
except Exception as e: | ||
print("Error:", e) | ||
conn.rollback() | ||
|
||
# Read the CSV file into a pandas DataFrame | ||
df = pd.read_csv(csv_file) | ||
|
||
# Convert DataFrame to list of tuples | ||
data = [tuple(row) for row in df.to_numpy()] | ||
|
||
# Generate the SQL INSERT statement | ||
cols = ', '.join(df.columns) | ||
placeholders = ', '.join(['%s'] * len(df.columns)) | ||
sql = f"INSERT INTO {table_name} ({cols}) VALUES ({placeholders})" | ||
|
||
# Execute the SQL statement to insert data into the table | ||
cursor.executemany(sql, data) | ||
|
||
# Commit the transaction | ||
conn.commit() | ||
|
||
print("Data uploaded successfully") | ||
|
||
except (Exception, Error) as e: | ||
print(f"Error uploading data to PostgreSQL: {e}") | ||
|
||
finally: | ||
# Close the cursor and connection | ||
if conn: | ||
cursor.close() | ||
conn.close() | ||
|
||
|
||
csv_file = "./list.csv" | ||
|
||
table_name = "student_list_2023" | ||
|
||
upload_csv_to_pg(csv_file, table_name) |