-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_db.py
More file actions
33 lines (29 loc) · 1.05 KB
/
Copy pathquery_db.py
File metadata and controls
33 lines (29 loc) · 1.05 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
import mysql.connector
try:
conn = mysql.connector.connect(
host="localhost",
user="jointliving",
password="jointliving",
database="jointliving"
)
cursor = conn.cursor()
tables = ["group_purchase", "board", "comment", "group_purchase_category", "users"]
for t in tables:
print(f"\n--- Table: {t} ---")
try:
if t == "users":
cursor.execute(f"SELECT id, username, email FROM {t} LIMIT 10")
elif t == "group_purchase_category":
cursor.execute(f"SELECT id, name FROM {t} LIMIT 10")
elif t == "comment":
cursor.execute(f"SELECT id, content FROM {t} LIMIT 10")
else:
cursor.execute(f"SELECT id, title FROM {t} LIMIT 10")
rows = cursor.fetchall()
for row in rows:
print(row)
except Exception as e:
print(f"Error querying {t}: {e}")
conn.close()
except Exception as e:
print(f"Connection error: {e}")