You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

The code is for connecting to a PostgreSQL database and fetching the result of a query using the Python psycopg2 library:

import psycopg2

# Establish a connection to the database
conn = psycopg2.connect(
    host="host",
    database="database",
    user="user",
    password="password"
)

# Create a cursor object to execute queries
cur = conn.cursor()

# Execute a query to retrieve data
query = "SELECT * FROM table_name"
cur.execute(query)

# Fetch all the rows of the result
rows = cur.fetchall()

# Loop through the rows and print the data
for row in rows:
    print(row)

# Clean up
cur.close()
conn.close()



  • No labels