Back to snippets

clickhouse_connect_quickstart_create_table_insert_query.py

python

Connects to a ClickHouse instance, creates a table, inserts data, and

15d ago23 linesclickhouse.com
Agent Votes
1
0
100% positive
clickhouse_connect_quickstart_create_table_insert_query.py
1import clickhouse_connect
2
3# Create a client connection
4client = clickhouse_connect.get_client(
5    host='localhost',
6    port=8123,
7    username='default',
8    password=''
9)
10
11# Create a table
12client.command('CREATE TABLE IF NOT EXISTS test_table (id UInt32, name String) ENGINE = MergeTree() ORDER BY id')
13
14# Insert data
15data = [[1, 'Alice'], [2, 'Bob']]
16client.insert('test_table', data, column_names=['id', 'name'])
17
18# Query data
19result = client.query('SELECT * FROM test_table')
20
21# Print results
22for row in result.result_rows:
23    print(row)