Search code examples
pythonkdbqpython

How to insert a row into KDB using qPython


I'm trying to use a script to connect to a KDB and insert rows into a table in the KDB using qpython (https://github.com/exxeleron/qPython). My table has the following column types:
"symbol","symbol","int","timestamp","string","string","symbol","symbol","string","string","string"

I tried using '.u.upd' but this returns nothing and does not update the table:

time = [numpy.timedelta64((numpy.datetime64(datetime.now()) - today), 'ms') for x in range(1)]
row = [qlist(['test'], qtype=QSYMBOL_LIST), qlist(['test'], qtype=QSYMBOL_LIST), qlist([1], qtype=QINT_LIST), qlist(time, qtype=QTIME_LIST),
        qlist([['t','e','s','t']], qtype=QSTRING_LIST), qlist([['t','e','s','t']], qtype=QSTRING_LIST), qlist(['test'], qtype=QSYMBOL_LIST), qlist(['test'], qtype=QSYMBOL_LIST),
        qlist([['t','e','s','t']], qtype=QSTRING_LIST), qlist([['t','e','s','t']], qtype=QSTRING_LIST), qlist([['t','e','s','t']], qtype=QSTRING_LIST)]

result = self.q.sendSync('.u.upd', numpy.string_('tableName'), row)

When I try using insert I get the error 'type:

result = self.q('tableName insert (`test;`test;1i;2019.08.09D12:00:00.123123123;("t"; "e"; "s"; "t");("t"; "e"; "s"; "t");`test;`test;("t"; "e"; "s"; "t");("t"; "e"; "s"; "t");("t"; "e"; "s"; "t"))')

Any help is appreciated.


Solution

  • You should pass the tablename by reference not by value when using insert, .i.e.

    result = self.q('`tableName insert (`test;`test;1i;2019.08.09D12:00:00.123123123;("t"; "e"; "s"; "t");("t"; "e"; "s"; "t");`test;`test;("t"; "e"; "s"; "t");("t"; "e"; "s"; "t");("t"; "e"; "s"; "t"))')

    Note the backtick in front of the tablename.

    For your other issue, .u.upd specifically only exists in a realtime/tickerplant setup and is not a built-in q function.