-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_project_sql.py
More file actions
42 lines (30 loc) · 1.01 KB
/
run_project_sql.py
File metadata and controls
42 lines (30 loc) · 1.01 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
#created with help of chatgpt -> minor modifications by Savana Hughes
import psycopg2
import sys
# Replace these variables with your PostgreSQL credentials
db_host = "127.0.0.1"
db_name = "final"
db_user = "postgres"
db_password = "postgres"
try:
# Connect to the PostgreSQL database
connection = psycopg2.connect(
host=db_host,
database=db_name,
user=db_user,
password=db_password
)
# Create a cursor to execute SQL queries
cursor = connection.cursor()
# Read and execute SQL commands from a .sql file
sql_file_path = '/Users/savana/Desktop/Clean Desktop/school/project/final/final_dml.sql'
with open(sql_file_path, 'r') as sql_file:
sql_commands = sql_file.read()
cursor.execute(sql_commands)
# Commit the changes and close the cursor and connection
connection.commit()
cursor.close()
connection.close()
print("SQL file executed successfully.")
except Exception as e:
print("An error occurred:", str(e))