-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_softwares.py
57 lines (46 loc) · 1.57 KB
/
import_softwares.py
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
import pandas as pd
import psycopg2
from sqlalchemy import create_engine
def import_softwares():
try:
# Read the CSV file
df = pd.read_csv('softwares.csv')
# Create database connection
engine = create_engine('postgresql://YOUR USER NAME:YOUR PASSWORD@localhost:5432/postgres')
# Create table and import data
df.to_sql('softwares',
engine,
if_exists='replace', # Replace if table exists
index=False)
# Verify the import
conn = psycopg2.connect(
dbname='postgres',
user='YOUR USER NAME',
password='YOUR PASSWORD',
host='localhost',
port='5432'
)
cur = conn.cursor()
# Get row count
cur.execute("SELECT COUNT(*) FROM softwares")
count = cur.fetchone()[0]
# Get column names
cur.execute("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'softwares'
""")
columns = [col[0] for col in cur.fetchall()]
print(f"\nSuccessfully created softwares table!")
print(f"Imported {count} rows")
print(f"Columns: {', '.join(columns)}")
cur.close()
conn.close()
except Exception as e:
print(f"Error: {str(e)}")
if 'cur' in locals():
cur.close()
if 'conn' in locals():
conn.close()
if __name__ == "__main__":
import_softwares()