【问题标题】:Cannot parse expression of type DateTime无法解析 DateTime 类型的表达式
【发布时间】:2021-12-15 19:39:06
【问题描述】:

如果我的问题有误,请在 cmets 中纠正我

我正在尝试将测试数据插入 clickhouse。出于测试目的,我使用 datetime.now(100% 正确的日期时间)

在我的代码的最后一行我有一个错误:

clickhouse_driver.errors.ServerException: Code: 62.
DB::Exception: Cannot parse expression of type DateTime here: 2021-11-01 00:19:12.933220, 2021-11-01 00:19:12.933237, 1,2,3,4,5, "TYPE_K", "BTCUSDT");
. Stack trace:

我该如何解决?

from clickhouse_driver import Client

import os
from datetime import datetime

client = Client.from_url(f'clickhouse://default:{os.getenv("CLICK_PASSWORD")}@localhost:9000/crypto_exchange')
print(client.execute('SHOW TABLES'))

# field names from binance API
client.execute('''
CREATE TABLE IF NOT EXISTS historical_data_binance
(
    dateTime DateTime,
    closeTime DateTime,
    open Float64,
    high Float64,
    low Float64,
    close Float64,
    volume Float64,
    kline_type String,
    ticker String
) ENGINE = Memory
''')

client.execute(f'''
INSERT INTO crypto_exchange.historical_data_binance (*) VALUES
({datetime.now()}, {datetime.now()}, 1,2,3,4,5, "TYPE_K", "BTCUSDT");
''')

【问题讨论】:

  • 如果我的问题是错误的,请纠正我
  • 与任何其他 DBMS 一样,需要引用。
  • 谢谢,我添加了引号“{datetime.now()}”、“{datetime.now()}”,但收到错误 -- Missing columns: '2021-11-01 00:55 :28.455322' 同时处理查询:'2021-11-01 00:55:28.455322'
  • @vladimir 谢谢你,但对于字符串 ('{datetime.now()}', '{datetime.now()}', 1,2,3,4,5, 'TYPE_', 'BTCUSDT');我有 --- DB::Exception: Cannot parse string '2021-11-01 01:21:48.160583' as DateTime: 位置 19 的语法错误(仅解析 '2021-11-01 01:21:48'):在执行 'FUNCTION CAST(_dummy_0, 'DateTime') DateTime = CAST(_dummy_0, 'DateTime')' 时。

标签: python database clickhouse


【解决方案1】:

我的解决方案

def prepare_table():
    client = clickhouse_driver.Client.from_url(f'clickhouse://default:{os.getenv("CLICK_PASSWORD")}@localhost:9000/crypto_exchange')
    
    # field names from binance API
    client.execute('''
CREATE TABLE IF NOT EXISTS historical_data_binance
(
    dateTime DateTime,
    closeTime Int64,
    open Float64,
    high Float64,
    low Float64,
    close Float64,
    volume Float64,
    kline_type String,
    ticker String
) ENGINE = Memory
''')
    return client



def insert_data(client, insert_data, db_name="crypto_exchange", table_name="historical_data_binance"):
    """
    insert_data = {
        "dateTime": dateTime, 
        "closeTime": closeTime, 
        "open": open,
        "high": hign,
        "low": low,
        "close": close,
        "volume": volume,
        "kline_type": kline_type,
        "ticker": ticker
    }
    """
    columns = ', '.join(insert_data.keys())
    query = 'insert into {}.{} ({}) values'.format(db_name, table_name, columns)
    
    data = []
    data.append(insert_data)
    client.execute(query, data)

【讨论】:

    猜你喜欢
    • 2014-11-04
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2017-03-28
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多