# Read and Write to a Rudimentary Database from C++

**URL:** <https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [February 4, 2015, 6:19pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435 "2015-02-04T18:19:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_L\_V](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@A\_L\_V](https://forum.kx.com/u/A_L_V)\
**Post date:** [February 4, 2015, 6:19pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/1 "2015-02-04T18:19:00Z")

</div>

My goal is to be able to read and write to a database (Kdb) from C++ code.

This question seems to have been addressed numerous times on the forums, but never in an entirely satisfactory (read “do A B C and it will work” :D) manner.

1. I have the header file **k.h** (http://code.kx.com/wsvn/code/kx/kdb%2B/c/c/k.h).
2. I have the example file **csv.c** (http://code.kx.com/wsvn/code/contrib/wiki/csv.c).

(2) confuses me slightly, specifically the line ( **localhost**?, **username**?, **password**?):

**int row,col,nCols,nRows,handle=khpu(“localhost”,12001,“myusername:mypassword”);**

I have been playing with Kdb+, have a table " **tabTrades**" that contains a list (vector) " **trades**" with 680 entries. I haven’t managed to save the database yet and have to create in the console every time I launch q.

I would like send a query " **tabTrades**" from C++ and get an output that shows the vector.

I have checked out the following links:  
http://code.kx.com/wiki/Cookbook/InterfacingWithC  
http://www.timestored.com/kdb-guides/kdb-c-api-example  
http://code.kx.com/wiki/Startingkdbplus/contents  
https://groups.google.com/forum/#!topic/personal-kdbplus/23KCuiCCkPA  
http://kx.com/q/c/c/readme.txt  
http://code.kx.com/wiki/Startingkdbplus/tables

Pointers and tips or even a full “recepie” would be much appreciated.

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [February 4, 2015, 6:31pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/2 "2015-02-04T18:31:00Z")

</div>

**\> int row,col,nCols,nRows,handle=khpu(“localhost”,12001,“myusername:mypassword”);**

is just multiple declarations and an init on one line. Could be written as

**int row;**

**int col;**

**int nCols;**

**int nRows;**

**int handle;**

**handle=khpu(“localhost”,12001,“myusername:mypassword”);**

but if that’s confusing you, you are in for some fun with c++ programming ;-)

This example connects to kdb+ via tcp, so you specify the hostname, port and username and password.

To start kdb+ listening on port 5000, start q as

q -p 5000

to save your small tabTrades table to disk, you can do

q)`:tabTrades set tabTrades

to load that from disk into variable tabTrades, just do

q)tabTrades:get`:tabTrades

You can still use the csv.c as an example

&nbsp; result=k(handle,“ **tabTrades** ”,(K)0);

Hope this helps,

Charlie

---

<div class="post-metadata">

**Author:** ![A\_L\_V](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@A\_L\_V](https://forum.kx.com/u/A_L_V)\
**Post date:** [February 5, 2015, 9:23am UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/3 "2015-02-05T09:23:00Z")

</div>

Thank you, Charles. The C++ example requires a username and a passsword.

Is there a way to launch Kdb to create them in addition to the port number, e.g. something like  
**q -p 5001 -u name:password** (unfortunately this dummy syntax does not work).

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [February 5, 2015, 9:41am UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/4 "2015-02-05T09:41:00Z")

</div>

the username:password supplied in the khpu call is relevant only if using authentication. By default, kdb+ does not use authentication, so the username:password could just be chosen to be something to identify your client app.

you can setup username:password pairs in a text file, e.g. u.txt, as

verminburger:verminspassword

one entry per line. This file can then be referenced via the cmd line -u option

q -u u.txt -p 5000

the password can also be stored as an md5 digest

http://code.kx.com/wiki/Reference/md5

q)md5"verminspassword"

0xb7096af841afbf9c1b8e7c4b706cff8c

so the entry in u.txt would then be

verminburger:b7096af841afbf9c1b8e7c4b706cff8c

---

<div class="post-metadata">

**Author:** ![A\_L\_V](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@A\_L\_V](https://forum.kx.com/u/A_L_V)\
**Post date:** [February 5, 2015, 9:54am UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/5 "2015-02-05T09:54:00Z")

</div>

Hi Charles. This is excellent information! If the username:password are not mandatory I would prefer keep things simple for the time being and change the C++ code to  
**int row,col,nCols,nRows,handle=khpu(“localhost”,5000);** to reflect that.

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [February 5, 2015, 9:59am UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/6 "2015-02-05T09:59:00Z")

</div>

ok, then you should use khp not khpu.  
**handle=khp(“localhost”,5000);**&nbsp;t

---

<div class="post-metadata">

**Author:** ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)\
**Post date:** [February 5, 2015, 10:02am UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/7 "2015-02-05T10:02:00Z")

</div>

Just don’t use authentication - use khp():

&nbsp;&nbsp;**handle=khp(“localhost”,5001)**

If you want to use authentication later…

I couldn’t find info on the password file on the wiki, but here’s a start:

https://groups.google.com/d/topic/personal-kdbplus/gO6wN\_UP1\_s/discussion

Ta, Jack.

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [February 5, 2015, 10:09am UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/8 "2015-02-05T10:09:00Z")

</div>

it’s kind of a red-herring. The username:password will be ignored by the kdb+ process if it is not using authentication. For good practice, maybe for consideration later, client applications should always provide a username that easily identifies that client application from the server side (visible with .z.u).

khp is just a wrapper for khpu that defaults u to “”. So this is equivalent:

**handle=khpu(“localhost”,5000,“”);**

and acts as a reminder for later that username and password are missing.&nbsp;

---

<div class="post-metadata">

**Author:** ![A\_L\_V](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@A\_L\_V](https://forum.kx.com/u/A_L_V)\
**Post date:** [February 5, 2015, 12:05pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/9 "2015-02-05T12:05:00Z")

</div>

I will keep the **khpu**. I am having problems compiling the **csv.c** though – “an undefiened reference to” **khpu** (and a number of other functions **k** , **kclose** etc). In the header file they are labelled **external** , which would suggest they are defined in a separate \*.cpp. I searched http://code.kx.com/wsvn/code/kx/kdb%2B/c/c/?#a13bd25ee24546baceff884a3804f0ecc but did not really see any definitions of the above mentioned functions.

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [February 5, 2015, 12:07pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/10 "2015-02-05T12:07:00Z")

</div>

the comments at the top of the csv.c show which file to link with. Those functions are present in c.o/c.dll. Choose whichever one for your target platform.

// obtain k.h from http://kx.com/q/c/c/k.h  
// linux:  
// &nbsp;compile with gcc -m64 -DKXVER=3 csv.c c.o  
// &nbsp;obtain c.o from http://kx.com/q/l64/c.o for linux  
// windows:  
// &nbsp;start the x86 or 64 bit version of build environment, then: cl -DKXVER=3 /MD csv.c c.obj ws2\_32.lib  
// &nbsp;obtain c.obj from http://kx.com/q/w32/ or w64/

---

<div class="post-metadata">

**Author:** ![A\_L\_V](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@A\_L\_V](https://forum.kx.com/u/A_L_V)\
**Post date:** [February 5, 2015, 1:57pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/11 "2015-02-05T13:57:00Z")

</div>

I am working with a NetBeans IDE; was perplexed a little by the c.o file mentioned in the comment (as it is in between a c.cpp and a c.exe). Will try placing all three files in a folder and compiling from the terminal. Thank you.

---

<div class="post-metadata">

**Author:** ![A\_L\_V](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@A\_L\_V](https://forum.kx.com/u/A_L_V)\
**Post date:** [February 5, 2015, 2:52pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/12 "2015-02-05T14:52:00Z")

</div>

Moved all the files (k.h, csv.c and c.o) in a single folder. Ran **gcc -m64 -DKXVER=3 csv.c c.o** , but still got a number of undefined functions: **sem\_post** , …, **pthread\_rwlock\_rdlock** etc. :(&nbsp;

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [February 5, 2015, 2:56pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/13 "2015-02-05T14:56:00Z")

</div>

link with the pthread library

**gcc -m64 -DKXVER=3 csv.c c.o** &nbsp;-lpthread

---

<div class="post-metadata">

**Author:** ![A\_L\_V](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@A\_L\_V](https://forum.kx.com/u/A_L_V)\
**Post date:** [February 5, 2015, 3:35pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/14 "2015-02-05T15:35:00Z")

</div>

Thank you, Charles. That worked. When running the executable for a query **tabTrades** in **a.out** I got a **type 0** output. Then tried running a different query for an example given in http://code.kx.com/wiki/A\_Brief\_Introduction\_to\_kdb%2B. It gave me **type 16 not supported by this client,banana,80,chicago**. So some of the information can be retrieved.

---

<div class="post-metadata">

**Author:** ![mark\_rooney](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@mark\_rooney](https://forum.kx.com/u/mark_rooney)\
**Post date:** [February 5, 2015, 3:58pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/15 "2015-02-05T15:58:00Z")

</div>

Hi,

The type 16 is a timestamp which is not supported in that code. The switch statement below in the csv.c  
code shows which types are supported:

switch(obj-\>t)  
{  
 case(0):{ // handle a list of char vectors  
 K x=kK(obj)[row];  
 if(10==x-\>t){int i;for(i=0;i\<xn;i++)printf("%c",kG(x)[i]);}  
 else printf("type %d not supported by this client",obj-\>t);  
 }break;  
 case(1):{printf("%d",kG(obj)[row]);}break;  
 case(4):{printf("%d",kG(obj)[row]);}break;  
 case(5):{printf("%d",kH(obj)[row]);}break;  
 case(6):{printf("%d",kI(obj)[row]);}break;  
 case(7):{printf("%lld",kJ(obj)[row]);}break;  
 case(8):{printf("%f",kE(obj)[row]);}break;  
 case(9):{printf("%f",kF(obj)[row]);}break;  
 case(11):{printf("%s",kS(obj)[row]);}break;  
 default:{printf("type %d not supported by this client",obj-\>t);}break;  
 }

## Thanks

Mark Rooney  
Financial Software Developer  
AQUAQ Analytics

---

<div class="post-metadata">

**Author:** ![A\_L\_V](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@A\_L\_V](https://forum.kx.com/u/A_L_V)\
**Post date:** [February 5, 2015, 4:50pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/16 "2015-02-05T16:50:00Z")

</div>

Thanks Mark! Added this **case(16):{printf(“%lld”,kJ(obj)[row]);}break;** to the code, which solved the second problem. Will see if I can solve the **type 0** problem.

N.B. This is a great active and knowledgeable google group.

---

<div class="post-metadata">

**Author:** ![mark\_rooney](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@mark\_rooney](https://forum.kx.com/u/mark_rooney)\
**Post date:** [February 5, 2015, 6:21pm UTC](https://forum.kx.com/t/read-and-write-to-a-rudimentary-database-from-c/9435/17 "2015-02-05T18:21:00Z")

</div>

This document may help you parse the different types of K objects from your C code:

http://www.aquaq.co.uk/wp-content/uploads/2015/01/AquaQ-Analytics-Transferring-Data-between-kdb+-and-C-V1.0.pdf

## Thanks

Mark Rooney  
Financial Software Developer  
AQUAQ Analytics
