Hiren Patel
Hiren Patel

Reputation: 1157

How to insert record in KDB (KX Database)

I have created one table named as test in KDB using following statement

test:([doc_id:`int$()];doc_displayid:`symbol$();doc_created_date:`date$();doc_aacess_date:`timestamp$();is_native_exist:`boolean$();file_size:`real$())

Now I want to insert a record in this

I've tried many ways like

  1. insert['test; (1;`D_30;.z.d;.z.P;T;8.5)]
  2. insert['test ([];`D_30;2018.8.8;2018.8.9T12:00:00.123;T;8.5)]
  3. insert['test (1;`D_30;.z.d;2018.8.9T12:00:00.123;T;8.5)]
  4. insert['test (1;`D_30;.z.d;2018.8.9T12:00:00.123;T;8.5)]
  5. 'test insert (1;`D_30;2018.8.8;2018.7.8T12:00:00.123;T;8.5)

But it doesn't work.

So please help me to solve this problem.

Thanks in advance.

Upvotes: 2

Views: 4088

Answers (2)

nyi
nyi

Reputation: 3229

There are multiple issues with your insert statements, please check out the KDB datatypes wiki for examples

  1. symbol should be defined with ` (uptick) i.e. `test , should not be 'test

  2. doc_id is defined as int, so you need to pass an explicit i .e.g. 1i, 2i

  3. There is no T boolean variable defined.

  4. Real should be defined with an explicit e at the end. (8.5e)

  5. timestamp is defined as dateDtimespan (D)

  6. You can use either insert or upsert. upsert allows you to overwrite the record for already inserted record, however, the insert will ensure that you always enter a unique key, otherwise, it will throw an error.


upsert[`test; (1i;`D_30;.z.d;.z.P;0b;8.5e)] 
insert[`test ;(2i;`D_30;2018.08.08;2018.08.09D12:00:00.123123123;0b;8.5e)]

doc_id| doc_displayid doc_created_date doc_aacess_date               is_native_exist file_size
------| --------------------------------------------------------------------------------------
1     | D_30          2018.08.21       2018.08.21D07:33:40.630975000 0                8.5
2     | D_30          2018.08.08       2018.08.09D12:00:00.123123123 0               8.5

Upvotes: 2

JejeBelfort
JejeBelfort

Reputation: 1663

Check the type of your input variables before insert in your test table. Basically:

  • your 1 is of type long, not int;
  • 2018.07.08T12:00:00.123 is of type datetime, not timestamp;
  • T does not exist, for boolean you should write 1b for true;
  • 8.5 is of type float, not real.

When converting the above to the appropriate format, the insert works provided that you use backtick `test not 'test:

`test insert (1i;`D_30;2018.08.08;"p"$2018.07.08T12:00:00.123;1b;8.5e)


doc_id| doc_displayid doc_created_date doc_aacess_date               is_native_exist file_size
------| --------------------------------------------------------------------------------------
1     | D_30          2018.08.08       2018.07.08D12:00:00.123000000 1               8.5      

Upvotes: 5

Related Questions