-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueries.py
39 lines (30 loc) · 876 Bytes
/
queries.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
import os
import psycopg2
from psycopg2 import sql
# Replace these with your actual database connection details
db_params = {
"dbname": "cheerup",
"user": os.environ["DB_KEY"],
"password": os.environ["DB_KEYP"],
"host": "database-1.ceenqhnjmvnk.us-west-2.rds.amazonaws.com",
"port": "5432",
}
# Establish a connection to the database
conn = psycopg2.connect(**db_params)
print(conn)
# Create a cursor object to execute SQL queries
cursor = conn.cursor()
# Example: Searching for data in a table
table_name = "locations"
# Construct the SQL query
query = sql.SQL("SELECT * FROM {};").format(sql.Identifier(table_name))
# Execute the query with the search value
cursor.execute(query)
# Fetch the results
results = cursor.fetchall()
# Print the results
for row in results:
print(row)
# Close the cursor and connection
cursor.close()
conn.close()