Hi,
Two questions
1.. I want to know if the table exists,if not I want to create a table.(table=typed table)
- In order to save a table ,save command would create a file on the disk.But it creates a new file and removes the old content.How can I append to the existing file
Thanks.
Hi,
The upsert function will solve both of those in one go. It will create a table if it doesn’t exist and append to the if it already exists.
q)key `:/db //returns nothing indicating trade doesn’t exist in db
q)
q)
q):/db/trade upsert ([]time:4?.z.t;sym:4?
JPM`GE;price:4?10f)
`:/db/trade //table has been created
q)
q)get `:/db/trade
time sym price
12:25:01.416 JPM 4.101914
08:34:04.468 JPM 2.337549
01:02:27.466 JPM 7.125845
02:24:40.646 GE 1.392257
q)
q)
q):/db/trade upsert flip(5?.z.t;5?
BP`AAPL;5?20f) //this will append to the table without overwriting
`:/db/trade
q)
q)
q)get `:/db/trade
time sym price
12:25:01.416 JPM 4.101914
08:34:04.468 JPM 2.337549
01:02:27.466 JPM 7.125845
02:24:40.646 GE 1.392257
12:12:45.199 BP 19.59219
09:44:09.076 AAPL 6.154981
01:51:55.498 BP 7.304546
13:05:09.588 AAPL 19.18235
07:23:39.994 BP 8.243732
Note: If the table you are appending to is a keyed table, then upsert will overwrite the key if it already exixts. In the case you don’t want that, you can use the insert function which will throw an error if that key already exists.
Regards,
Jason
Jason Fealy | Kx Engineer | Kx | +1 (718) 340-9791 | jfealy@kx.com