2022.07.12 07:53 AM - edited 2022.07.12 07:54 AM
This is about qpython code. Let's say we have a table named "t" with 2 columns "name" and "iq" in kdb:
from qpython import qconnection, qcollection
q = qconnection.QConnection(host='localhost', port=5000):
q.open()
q("t:flip `name`iq!(`Dent`Beeblebrox`Prefect;98 42 126)")
How can we pass the column name and table name through qpython? Thanks.
pseudo code:
q({select x from y}, "iq", "t")
2022.07.12 08:23 AM - edited 2022.07.13 02:04 AM
Some ways:
q("{((),x)#y}", numpy.string_("iq"), numpy.string_("t"))
q("{value\"select \",string[x],\"from \",string y}", "iq", "t")
q("select %s from %s" % ("iq","t"))
q("{?[y;();0b;{x!x}(),x]}", numpy.string_("iq"), numpy.string_("t"))
Functional selects are the most powerful way https://code.kx.com/q/wp/parse-trees/
2022.07.12 09:14 AM - edited 2022.07.12 09:15 AM
Thanks @rocuinneagain. I was trying the functional select and have to convert the strings to <class 'numpy.bytes_'> first (I am using python 3), i.e.
q("{?[y;();0b;{x!x}(),x]}", np.bytes_("iq"), np.bytes_("t"))
2022.07.12 10:28 AM - edited 2022.07.13 06:53 AM
See docs for more on how the library handles difference between symbols and strings:
https://qpython.readthedocs.io/en/latest/type-conversion.html#string-and-symbols
(Answer above updated to use numpy.string_
to pass symbols as needed)
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.