Question> What is the correct way to insert values into clickhouse tables within bash script?
The table structure is as follows:
$ clickhouse-client --query="create table default.hello(product String, qty UInt32) ENGINE=MergeTree partition by tuple() order by tuple()" --host=127.0.0.1 --user=user_name --password=user_pass
$ clickhouse-client --query="insert into table default.hello values ('a', 10), ('b', 20)" --host=127.0.0.1 --user=user_name --password=user_pass
$ clickhouse-client --query="select * from default.hello" --host=127.0.0.1 --user=user_name --password=user_pass
┌─product─┬─qty─┐
│ a │ 10 │
│ b │ 20 │
└─────────┴─────┘
However, I am not able to insert values into the table through bash script.
I have tried two methods, but none of them work for me.
$ cat hello.sh
###############################
#!/bin/bash
#INSERT_QUERY="INSERT INTO default.hello values('d', 13)"
#Code: 62. DB::Exception: Syntax error: failed at position 7 (end of query): . Expected INTO
#INSERT_QUERY="\"INSERT INTO default.hello values('e', 10)"\"
#Code: 62. DB::Exception: Syntax error: failed at position 1: "INSERT. Double quoted string is not closed
clickhouse-client --query=$INSERT_QUERY --host=127.0.0.1 --user=user_name --password=user_pass
###############################
I have no problems to run it through the command line.
$ clickhouse-client --query="INSERT INTO default.hello values('d', 10)" --host=127.0.0.1 --user=user_name --password=user_pass
ClickHouse client version 18.6.0.
Connected to ClickHouse server version 18.6.0 revision 54401.
:) select * from default.hello;
SELECT *
FROM default.hello
┌─product─┬─qty─┐
│ a │ 10 │
│ b │ 20 │
└─────────┴─────┘
┌─product─┬─qty─┐
│ d │ 10 │
└─────────┴─────┘