clickhouse-driver icon indicating copy to clipboard operation
clickhouse-driver copied to clipboard

Placeholders are not replaced with the actual values when using executemany

Open dmitriyshashkin opened this issue 5 years ago • 4 comments
trafficstars

Describe the bug I've noticed the problem when I tried to use pandas to_sql method but was able to reproduce the problem with clickhouse driver only.

To Reproduce Create the table CREATE TABLE dbg_test (foo String) ENGINE = Log;

Then:

from clickhouse_driver import connect
conn = connect('clickhouse+native://default:@localhost:9000/default')
cursor = conn.cursor()
cursor.executemany('insert into dbg_test (foo) values(%(foo_value)s)', [{'foo_value': 'bar'}, {'foo_value': 'bar2'}])

Expected behavior Two entries (bar, bar2) are inserted into the database.

Actual behavior DB::Exception: Cannot parse expression of type String here: %(foo_value)s).

Versions 0.1.3 python 3.6.9

dmitriyshashkin avatar Mar 27 '20 14:03 dmitriyshashkin

You should not use placeholders after VALUES clause in INSERT queries. The brief explanation is available in docs.

This case should be documented.

We can chop the rest of string after VALUES clause. But I don't think this is good idea.

xzkostyan avatar Apr 04 '20 15:04 xzkostyan

That's a pity, as I mentioned earlier I've encountered this problem when I tried to save pandas dataframe into CH. I don't have any control over the way pandas is passing params to the driver, so I guess I'll have to look for a workaround.

Perhaps you should raise an exception if someone is trying to use placeholders? I spent quite some time trying to understand while a routine operation on pandas dataframe causes an error.

dmitriyshashkin avatar Apr 10 '20 21:04 dmitriyshashkin

OK. We can try straightforward truncate.

@dmitriyshashkin can you try this solution?

xzkostyan avatar May 10 '20 15:05 xzkostyan

I'll give it a try early next week.

dmitriyshashkin avatar May 11 '20 08:05 dmitriyshashkin