Forums

Full Version: How to read data from clickhouse database using python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import pyodbc
import pandas as pd

# Connect to the ClickHouse database
conn = pyodbc.connect(
   'Driver={ClickHouse ODBC Driver};'
   'Server=localhost;'
   'Port=9000;'
   'Database=mydatabase;'
   'UID=user;'
   'PWD=password'
)

# Create a cursor
cursor = conn.cursor()

# Execute a SELECT statement to retrieve data from a table
cursor.execute('SELECT * FROM mytable')

# Fetch the rows from the cursor and create a DataFrame
df = pd.DataFrame.from_records(cursor.fetchall())

# Close the cursor and connection
cursor.close()
conn.close()

# Print the DataFrame
print(df)