-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_load_table.py
More file actions
180 lines (149 loc) · 6.42 KB
/
create_load_table.py
File metadata and controls
180 lines (149 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
import tkinter as tk
from tkinter import simpledialog, messagebox, filedialog
import csv
import psycopg2
import logging
import os
# Configure logging
logging.basicConfig(filename='data_loader.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# Function to check the database connection
def check_db_connection(dbname, user, password, host, port):
try:
conn = psycopg2.connect(
dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
conn.close()
return True
except psycopg2.OperationalError as e:
logging.error(f"Database connection failed: {e}")
return False
# Function to get new database connection details from the user input
def get_db_details():
dbname = simpledialog.askstring("Database Info", "Enter database name:", initialvalue="projectBloom")
user = simpledialog.askstring("Database Info", "Enter user:", initialvalue="postgres")
password = simpledialog.askstring("Database Info", "Enter password:", show='*')
host = simpledialog.askstring("Database Info", "Enter host:", initialvalue="localhost")
port = simpledialog.askstring("Database Info", "Enter port:", initialvalue="5432")
return dbname, user, password, host, port
# Function to select the CSV file
def select_csv_file():
file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
if file_path:
csv_path_entry.delete(0, tk.END)
csv_path_entry.insert(0, file_path)
# Function to run the script
def run_script():
csv_file_path = csv_path_entry.get()
table_name = table_name_entry.get()
schema_name = schema_name_entry.get() or "public"
delimiter = delimiter_var.get()
if not csv_file_path or not table_name:
messagebox.showwarning("Input Error", "Please provide both CSV file path and table name.")
return
if not os.path.isfile(csv_file_path):
messagebox.showwarning("File Error", "The CSV file does not exist or is not accessible.")
return
try:
# Database connection
conn = psycopg2.connect(
dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
cursor = conn.cursor()
logging.info("Database connection established.")
# Reading CSV headers
with open(csv_file_path, 'r') as f:
reader = csv.reader(f, delimiter=delimiter)
headers = next(reader)
# Check if the table exists
cursor.execute(f"""
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = %s AND table_name = %s
);
""", (schema_name, table_name))
table_exists = cursor.fetchone()[0]
if not table_exists:
# Create table if it doesn't exist
columns = ", ".join([f"{header} TEXT" for header in headers])
create_table_query = f"CREATE TABLE {schema_name}.{table_name} ({columns});"
cursor.execute(create_table_query)
logging.info(f"Table {schema_name}.{table_name} created.")
else:
# Truncate table if it exists
truncate_table_query = f"TRUNCATE TABLE {schema_name}.{table_name};"
cursor.execute(truncate_table_query)
logging.info(f"Table {schema_name}.{table_name} truncated.")
conn.commit()
# Loading data into the table
copy_sql = f"COPY {schema_name}.{table_name} FROM STDIN WITH CSV HEADER DELIMITER AS '{delimiter}'"
with open(csv_file_path, 'r') as f:
cursor.copy_expert(sql=copy_sql, file=f)
conn.commit()
logging.info(f"Data loaded successfully into {schema_name}.{table_name}.")
cursor.close()
conn.close()
messagebox.showinfo("Success", f"Data loaded successfully into PostgreSQL. Check {dbname}.{schema_name} schema")
app.quit()
except (Exception, psycopg2.DatabaseError) as e:
logging.error(f"An error occurred: {e}")
messagebox.showerror("Error", f"An error occurred: {e}")
# Create the main window
app = tk.Tk()
app.title("CSV Data Loader")
# Default database connection details
dbname = "projectBloom"
user = "postgres"
password = "523041"
host = "localhost"
port = "5432"
# Check the default database connection
if check_db_connection(dbname, user, password, host, port):
use_default = messagebox.askyesno("Database Connection", f"Connection to DB {dbname}, username {user} successful. Do you want to use these credentials?")
if not use_default:
dbname, user, password, host, port = get_db_details()
else:
messagebox.showwarning("Database Connection", "Could not connect with the default settings. Please provide new connection details.")
dbname, user, password, host, port = get_db_details()
# Instructions label
tk.Label(app, text="Enter CSV file path, table name, and schema (optional), then click the Run button to load data.").pack(pady=10)
# CSV file path entry
tk.Label(app, text="CSV File Path:").pack(pady=5)
csv_frame = tk.Frame(app)
csv_frame.pack(pady=5)
csv_path_entry = tk.Entry(csv_frame, width=50)
csv_path_entry.pack(side=tk.LEFT, padx=5)
csv_browse_button = tk.Button(csv_frame, text="Browse", command=select_csv_file)
csv_browse_button.pack(side=tk.RIGHT, padx=5)
# Table name entry
tk.Label(app, text="Table Name:").pack(pady=5)
table_name_entry = tk.Entry(app, width=50)
table_name_entry.pack(pady=5)
# Schema name entry (optional)
tk.Label(app, text="Schema Name (Optional, default is 'public'):").pack(pady=5)
schema_name_entry = tk.Entry(app, width=50)
schema_name_entry.pack(pady=5)
# Delimiter selection
tk.Label(app, text="Select Delimiter:").pack(pady=5)
delimiter_var = tk.StringVar(value=',') # Default to comma
delimiter_frame = tk.Frame(app)
delimiter_frame.pack(pady=5)
tk.Radiobutton(delimiter_frame, text="Comma (,)", variable=delimiter_var, value=',').pack(side=tk.LEFT)
tk.Radiobutton(delimiter_frame, text="Pipe (|)", variable=delimiter_var, value='|').pack(side=tk.LEFT)
tk.Radiobutton(delimiter_frame, text="Tilde (~)", variable=delimiter_var, value='~').pack(side=tk.LEFT)
# Run button
run_button = tk.Button(app, text="Run", command=run_script)
run_button.pack(pady=10)
tk.Label(app, text="CSV to PostgreSQL Data Loader \nCreated by Sumit").pack(pady=5)
# Start the GUI event loop
app.mainloop()