# upsert example by C interface

**URL:** <https://forum.kx.com/t/upsert-example-by-c-interface/5799>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [September 22, 2008, 9:07pm UTC](https://forum.kx.com/t/upsert-example-by-c-interface/5799 "2008-09-22T21:07:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbtouch](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@dbtouch](https://forum.kx.com/u/dbtouch)\
**Post date:** [September 22, 2008, 9:07pm UTC](https://forum.kx.com/t/upsert-example-by-c-interface/5799/1 "2008-09-22T21:07:00Z")

</div>

Received: by 10.150.51.6 with SMTP id y6mr186765yby.24.1222114067116; Mon, 22  
Sep 2008 13:07:47 -0700 (PDT)  
Date: Mon, 22 Sep 2008 13:07:47 -0700 (PDT)  
X-IP: 148.106.2.6  
User-Agent: G2/1.0  
X-Google-Token: d6T62gwAAAC5RQSrv4E0yFsP5ThxjZJv  
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET  
CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR  
3.0.04506.648),gzip(gfe),gzip(gfe)  
Message-ID:   
Subject: upsert example by C interface  
From: dbtouch   
To: “Kdb+ Personal Developers”   
X-Google-Approved: [niall.dalton@gmail.com](mailto:niall.dalton@gmail.com) via email at 2008-09-22 20:08:41  
  
Hi, KDB users  
  
Can you show me an example to do upsert using C interface? I tried the  
following example and the second upsert turned out to be insert.  
  
1. create table as:  
all\_dt:(seq:`int$();cchar:`char$();cshort:`short$();cint:`int$  
();cfloat:`float$();csym:`symbol$())  
  
2. compile and run this program:  
#include “k.h”  
// dml.cpp : simple insert  
//  
#include “k.h”  
#include “stdio.h”  
int main(int argc, char\* argv)  
{  
 K err;  
 I kdbSocketHandle = khp(“localhost”, 5001);  
 if (kdbSocketHandle \> 0)  
 {  
 // K row = knk(3, ks((S)“ibm”), kf (93.5), ki(300));  
 // k(-kdbSocketHandle, “.u.upd”, ks((S)“trade”), row, (K)0);  
 K row=knk(6, ki(1), kc(‘b’), kh(200), ki(2000), kf(99.9),  
ks((S)“hello world”));  
 err=k(-kdbSocketHandle, “upsert”, ks((S)“all\_dt”), row, (K)0);  
 K row2=knk(6, ki(1), kc(‘c’), kh(200), ki(2000), kf(99.9),  
ks((S)“hello world”));  
 err=k(-kdbSocketHandle, “upsert”, ks((S)“all\_dt”), row2, (K)0);  
 // K row=knk(3, ks((S)“q”), ki(10), ki(50));  
 // err=k(-kdbSocketHandle, “upsert”, ks((S)“a”), row, (K)0);  
 if (err-\>t==-128)  
 {  
 printf(“err: %s\n”, err-\>s);  
 }  
 k(kdbSocketHandle,“”,(K)0); // flush  
 }  
}  
  
3. show all\_dt in q session: I still see 2 rows:  
  
q)show all\_dt  
seq cchar cshort cint cfloat csym  
----------------------------------------  
1 b 200 2000 99.9 hello world  
1 c 200 2000 99.9 hello world

---

<div class="post-metadata">

**Author:** ![Attila](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Attila](https://forum.kx.com/u/Attila)\
**Post date:** [September 22, 2008, 9:10pm UTC](https://forum.kx.com/t/upsert-example-by-c-interface/5799/2 "2008-09-22T21:10:00Z")

</div>

X-Mailer: Apple Mail (2.929.2)

upsert is only different for keyed tables

should be

all\_dt:([seq:`int$()] cchar:`char$();cshort:`short$ ();cint:`int$();cfloat:`float$();csym:`symbol$())

Regards,  
Attila

---

<div class="post-metadata">

**Author:** ![dbtouch](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@dbtouch](https://forum.kx.com/u/dbtouch)\
**Post date:** [September 23, 2008, 2:27pm UTC](https://forum.kx.com/t/upsert-example-by-c-interface/5799/3 "2008-09-23T14:27:00Z")

</div>

Thanks, Attila. I will try it out. I thought I used keyed table in mysyntax.dbtouchOn Sep 22, 4:10?pm, Attila Vrabecz \<attila.vrab…\> wrote:\> upsert is only different for keyed tables\>\> should be\>\> all\_dt:([? ?seq:`int$() ?] ? ? cchar:`char$();cshort:`short$&gt; ();cint:`int$();cfloat:`float$();csym:`symbol$())\>\> Regards,\> ? ? Attila\>\> On 22 Sep 2008, at 21:07, dbtouch wrote:\>\>\>\>\>\> \> Hi, KDB users\>\> \> Can you show me an example to do upsert using C interface? I tried the\> \> following example and the second upsert turned out to be insert.\>\> \> 1. create table as:\> \> all\_dt:(seq:`int$();cchar:`char$();cshort:`short$();cint:`int$\> \> ();cfloat:`float$();csym:`symbol$())\>\> \> 2. compile and run this program:\> \> #include “k.h”\> \> // dml.cpp : simple insert\> \> //\> \> #include “k.h”\> \> #include “stdio.h”\> \> int main(int argc, char\* argv)\> \> {\> \> ? ?K err;\> \> ? ?I kdbSocketHandle = khp(“localhost”, 5001);\> \> ? ?if (kdbSocketHandle \> 0)\> \> ? ?{\> \> ? ? ? ? ? ?// K row = knk(3, ks((S)“ibm”), kf (93.5), ki(300));\> \> ? ? ? ? ? ?// k(-kdbSocketHandle, “.u.upd”, ks((S)“trade”), row, (K)0);\> \> ? ? ? ? ? ?K row=knk(6, ki(1), kc(‘b’), kh(200), ki(2000), kf(99.9),\> \> ks((S)“hello world”));\> \> ? ? ? ? ? ?err=k(-kdbSocketHandle, “upsert”, ks((S)“all\_dt”), row, (K)0);\> \> ? ? ? ? ? ?K row2=knk(6, ki(1), kc(‘c’), kh(200), ki(2000), kf(99.9),\> \> ks((S)“hello world”));\> \> ? ? ? ? ? ?err=k(-kdbSocketHandle, “upsert”, ks((S)“all\_dt”), row2, (K)0);\> \> ? ? ? ? ? ?// K row=knk(3, ks((S)“q”), ki(10), ki(50));\> \> ? ? ? ? ? ?// err=k(-kdbSocketHandle, “upsert”, ks((S)“a”), row, (K)0);\> \> ? ? ? ? ? ?if (err-\>t==-128)\> \> ? ? ? ? ? ?{\> \> ? ? ? ? ? ? ? ? ? ?printf(“err: %s\n”, err-\>s);\> \> ? ? ? ? ? ?}\> \> ? ? ? ? ? ?k(kdbSocketHandle,“”,(K)0); // flush\> \> ? ?}\> \> }\>\> \> 3. show all\_dt in q session: I still see 2 rows:\>\> \> q)show all\_dt\> \> seq cchar cshort cint cfloat csym\> \> ----------------------------------------\> \> 1 ? b ? ? 200 ? ?2000 99.9 ? hello world\> \> 1 ? c ? ? 200 ? ?2000 99.9 ? hello world- Hide quoted text -\>\> - Show quoted text -\</attila.vrab…\>
