首页 > 文章列表 > 如何使用 ClickHouse JS 在 Vue 项目中连接 ClickHouse 数据库?

如何使用 ClickHouse JS 在 Vue 项目中连接 ClickHouse 数据库?

446 2025-02-11

如何使用 ClickHouse JS 在 Vue 项目中连接 ClickHouse 数据库?

使用 clickhouse js 连接数据库

在 vue 项目中使用 clickhouse js 连接数据库有以下步骤:

  1. 封装

封装 clickhouse js 实例,创建连接池:

import clickhouse from 'clickhouse';

const pool = clickhouse.createpool({
  // 数据库连接参数
});
  1. 使用

使用创建的连接池进行增删改查:

连接数据库

const connection = await pool.getconnection();

查询数据

const result = await connection.query('select * from table_name');

插入数据

await connection.execute(
  'insert into table_name (id, name) values (?, ?)',
  [1, 'john doe']
);

更新数据

await connection.execute(
  'update table_name set name = ? where id = ?',
  ['jane doe', 1]
);

删除数据

await connection.execute('delete from table_name where id = 1');

释放连接

connection.release();
来源:1731366004