1. 安装clickhouse-driver库

可以使用pip命令安装clickhouse-driver库:

pip install clickhouse-driver
  1. 连接clickhouse数据库

使用clickhouse-driver库中的connect()函数连接clickhouse数据库,需要指定主机名、端口号、数据库名称、用户名和密码等参数:

from clickhouse_driver import Client

client = Client(host='localhost', port=8123, database='test', user='default', password='')
  1. 使用A表

使用client对象的execute()函数执行SQL语句,可以查询A表中的数据:

result = client.execute('SELECT * FROM A')
print(result)

完整代码如下:

from clickhouse_driver import Client

client = Client(host='localhost', port=8123, database='test', user='default', password='')

result = client.execute('SELECT * FROM A')
print(result)

注意:在使用clickhouse-driver库连接clickhouse数据库时,需要先在clickhouse服务器端开启TCP端口,可在配置文件中设置:

<tcp_port>8123</tcp_port>
``
如何用python连接clickhouse数据库并使用其中A表

原文地址: http://www.cveoy.top/t/topic/eP7v 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录