Forums
How to read data from clickhouse database using python? - Printable Version

+- Forums (https://bdn.bdb.ai)
+-- Forum: BDB Knowledge Base (https://bdn.bdb.ai/forumdisplay.php?fid=13)
+--- Forum: BDB Data Pipeline (https://bdn.bdb.ai/forumdisplay.php?fid=48)
+---- Forum: BDB Data Pipeline Q & A (https://bdn.bdb.ai/forumdisplay.php?fid=17)
+---- Thread: How to read data from clickhouse database using python? (/showthread.php?tid=581)



How to read data from clickhouse database using python? - shreekantgosavi - 12-23-2022

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)