首页 > 数据库 >clickhouse读写数据库3-- clickhouse_driver 模块

clickhouse读写数据库3-- clickhouse_driver 模块

时间:2023-06-07 19:13:21浏览次数:39  
标签:execute -- col2 driver records operation clickhouse

这是用 百度的 文心一言 生成的代码。
刚开始2次都是错误的,
明确指出 clickhouse_driver 没有占位符,
让AI重新生成。
重新生成了2次之后,才得到正确代码

 

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# author:henry
# desc:整理 clickhouse 读写的范例,方便日后读写clickhouse库
# Date:20230607
import clickhouse_driver


## 这是用 百度的 文心一言 生成的代码。刚开始2次都是错误的,明确指出 clickhouse_driver 没有占位符, 让AI重新生成。重新生成了2次之后,才得到正确代码
# 创建连接 '172.16.xx.xxx;user;password;dbname;utf8;9000'

host = '172.16.xx.xx'
user = 'user'
password = 'password'
db_name = 'dbname'

# 连接clickhouse数据库
conn = clickhouse_driver.connect(
    host= host,
    port=9000,
    user= user,
    password= password,
    database= db_name
)

# 创建游标
cursor = conn.cursor()

# 插入数据的SQL语句,使用占位符
sql = "INSERT INTO HENRY_TEST2_20230607 (col1, col2, col3) VALUES "

# 待插入的数据,每个元素是一个元组,包含三个值
data1 = {'col1':4, 'col2':'henry1', 'col3':11}
data2 = {'col1':5, 'col2':'henry2', 'col3':22}
data3 = {'col1':6, 'col2':'henry3', 'col3':33}

records = []
records.append(data1)
records.append(data2)
records.append(data3)


"""
def executemany(self, operation, seq_of_parameters):
    Prepare a database operation (query or command) and then execute it
    against all parameter sequences found in the sequence
    `seq_of_parameters`.

    :param operation: query or command to execute.
    :param seq_of_parameters: sequences or mappings for execution.
    :return: None
    
def execute(self, operation, parameters=None):
    Prepare and execute a database operation (query or command).

    :param operation: query or command to execute.
    :param parameters: sequence or mapping that will be bound to
                       variables in the operation.
    :return: None
"""
# 使用executemany方法批量插入数据. execute 也可以插入数据
cursor.executemany(sql, records)
#cursor.execute(sql, records)

# 提交事务
conn.commit()

# 关闭连接
conn.close()

"""
CREATE TABLE db_center.HENRY_TEST2_20230607
(    
    `col1` Int64,
    `col2` String,
    `col3` Int64
)
ENGINE = ReplacingMergeTree
ORDER BY (col2)
SETTINGS index_granularity = 8192;

"""

 

标签:execute,--,col2,driver,records,operation,clickhouse
From: https://www.cnblogs.com/music-liang/p/17464299.html

相关文章

  • 平台设备信息获取
    电池:FGenericPlatformMisc::GetBatteryLevelFAndroidMisc::GetBatteryLevelFIOSPlatformMisc::GetBatteryLevel制造厂商:都是FGenericPlatformMisc::GetDeviceMakeAndModel操作系统FPlatformMisc::GetOSVersionFAndroidMisc::GetOSVersionFIOSPlatformMisc::GetOSVersion内存都是......
  • Leetcode 2611. 老鼠和奶酪
    题目:有两只老鼠和 n 块不同类型的奶酪,每块奶酪都只能被其中一只老鼠吃掉。下标为i 处的奶酪被吃掉的得分为:如果第一只老鼠吃掉,则得分为 reward1[i] 。如果第二只老鼠吃掉,则得分为 reward2[i] 。给你一个正整数数组 reward1 ,一个正整数数组 reward2 ,和一个非负整......
  • D2C深度图对齐彩色图
    深度图对齐彩色图原理部分一般深度相机自带sdk会有对齐的函数,这些函数一般是硬件实现对齐,但是有些相机不支持高分辨率的对齐,比如Astraminis只支持最高640*480的分辨率对齐,所以考虑自己实现对齐函数。深度图与彩色图的配准与对齐深度相机和彩色相机对齐(d2c)Realsense相机SD......
  • CSI架构和原理
    CSICSI简介CSI的诞生背景K8s原生支持一些存储类型的PV,如iSCSI、NFS、CephFS等等,这些in-tree类型的存储代码放在Kubernetes代码仓库中。这里带来的问题是K8s代码与三方存储厂商的代码强耦合:更改in-tree类型的存储代码,用户必须更新K8s组件,成本较高in-tree存......
  • 后端获取TOKEN返oss上传地址,前端如何将字符串传给这个地址
    您也可以使用Axios库来发送请求,以下是发送表单请求的示例代码:constaxios=require('axios');functionuploadStringToOSS(ossPostData,content){constformData=newFormData();Object.keys(ossPostData.fields).forEach(key=>{formData.append(key,os......
  • ydwe 自动合成jass演示(代码有待优化)
    //公式库//1.要对每一个道具都先用物编做一个假道具//示例:id为I002的道具为I000(自定义+6攻爪)的假道具//示例:id为I00D的道具为I007(自定义食人鬼手套)的假道具//2.创建哈希表//3.由于jass没有类似于java的集合,所以此处为将数据重复的添加到哈......
  • vscode rest client
    安装略helloworld文档地址:https://marketplace.visualstudio.com/items?itemName=humao.rest-client文件后缀为.http或.rest一个文件有多个请求的话,用###分割如果有报错:HeadernamemustbevalidHTTPtoken,细看官网,则注意大小写content-typebody参......
  • JDK没有JAVAX.ANNOTATION.JAR包解决方案,无法使用@RESOURCE解决方案
    高版本JDK无法使用@Resource注解解决方案1.普通项目下载javax.annotation-api-1.3.2.jar,并在lib目录中引入即可2.Maven项目Maven项目:在pom.xml中进行配置<dependency><groupId>javax.annotation</groupId><artifactId>jsr250-api</artifactId><ver......
  • 数据结构与算法-队列
    队列FIFO先进先出队列的实现classQueue(object):def__init__(self):self.__list=[]defenqueue(self,item):self.__list.append(item)defdequeue(self):returnself.__list.pop(0)defis_empty():returnse......
  • Ubuntu和MIUI时间显示秒数字
    Ubuntu(23.04)状态栏的时间显示秒,则需要执行以下命令mango@mango-ubuntu:~/Desktop$gsettingssetorg.gnome.desktop.interfaceclock-show-secondstrue小米手机(MIUI13)要展示时间到分秒可以打开时间悬浮窗功能即可。1.打开手机设置点击更多设置。2.点击页面下方的开发......