cancel
Showing results for 
Search instead for 
Did you mean: 

qpython to select columns from table

powerpeanuts
New Contributor III

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")

 

3 REPLIES 3

rocuinneagain
Contributor III
Contributor III

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"))

 

 

  1. Using # https://code.kx.com/q/ref/take/
  2. Using value https://code.kx.com/q/ref/value/ 
  3. Python string formatting
  4. Functional select https://code.kx.com/q/basics/funsql/ 

Functional selects are the most powerful way https://code.kx.com/q/wp/parse-trees/ 

powerpeanuts
New Contributor III

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"))

 

  

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)